【问题标题】:Change fragments textview from activity从活动中更改片段文本视图
【发布时间】:2017-04-13 23:53:53
【问题描述】:

我想从活动中更改片段文本视图(不是片段类)。我该怎么做?我正在使用此代码:

public void startChat() {
    FrameLayout layout = (FrameLayout)findViewById(R.id.container);
    layout.setVisibility(View.VISIBLE); 
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.container, new ConversationFragment());
    fragmentTransaction.commit();
    viewPager.setVisibility(View.GONE);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayHomeAsUpEnabled(true);
    TextView nameView=(TextView)findViewById(R.id.user_name);
    nameView.setText("asd");
}

此代码加载了 conversation_fragment.xml 并想要更改 textview,但我的应用程序崩溃了。

这里是 conversation_fragment.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#878787" >

        <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="dfgdfgdf"
        android:textSize="20dp"
        android:layout_centerInParent="true"
        android:id="@+id/user_name"/>

    <EditText
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   />

    <Button 
        android:text="Gönder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getFromUser"
        android:layout_marginTop="40dp"
        />

</RelativeLayout>

【问题讨论】:

  • 你应该等待onCreateView结束,之后你就可以调用findViweById

标签: android android-fragments


【解决方案1】:

您可以使用findFragmentById 找到您的片段并调用更改文本的公共方法

(ConversationFragment) getFragmentManager().findFragmentById(R.id.yourid);

或在创建类时保留一个实例,然后再将其添加到片段管理器

conv = new ConversationFragment()
fragmentTransaction.add(R.id.container, conv);

然后只需使用 conv 或任何您调用的方法调用公共方法

编辑:

您使用捆绑包将数据发送到片段

Bundle b = new Bundle()
b.putString("text",data)
conv.setArguments(b);

然后在您的片段中使用getArguments() 获取参数并从包中提取数据并根据需要使用它

【讨论】:

  • 等一下,您是否在创建片段时尝试更改文本视图?
  • 我不明白这一行:b.putString("text",data) 我也必须更新图像视图我该怎么做?能举个例子吗?
【解决方案2】:

你可以这样做

public class ConversationFragment extends Fragment {

    private TextView nameView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.conversation_fragment, null);
        nameView = (TextView) view.findViewById(R.id.user_name);
    }

    public void setText(String yourText){
        nameView.setText(yourText); 
    }
}

在你的活动调用方法中setText()

ConversationFragment conv = new ConversationFragment();
conv.setText("asd");

致以最诚挚的问候。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2016-10-26
  • 2016-08-18
相关资源
最近更新 更多