怎么更新Fragment中的textview?
时间: 2024-03-25 10:37:34 浏览: 211
要更新Fragment中的TextView,可以通过以下步骤实现:
1.在Fragment的布局文件中找到TextView并设置一个id。
2.在Fragment的Java代码中,通过View.findViewById()方法获取TextView对象。
3.使用TextView.setText()方法更新TextView的文本。
例如,如果我们有一个Fragment布局文件fragment_example.xml,其中包含一个id为text_view的TextView,我们可以在Fragment的Java代码中使用以下代码更新TextView的文本:
```
public class ExampleFragment extends Fragment {
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_example, container, false);
textView = view.findViewById(R.id.text_view);
return view;
}
public void updateTextView(String text) {
textView.setText(text);
}
}
```
在这个例子中,我们在onCreateView()方法中使用findViewById()方法获取TextView对象,并在updateTextView()方法中使用setText()方法更新TextView的文本。
阅读全文
相关推荐


















