从Fragment设置textView文本将不起作用 [英] Set textView text from a Fragment won't work
本文介绍了从Fragment设置textView文本将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用'if first run'语句从片段内部设置一些文本, setText()方法不会设置任何文本,我无法解决原因。
Using the 'if first run' statement to set some text from inside a fragment, the setText() method doesn't set any text and I can't work out why.
public class TabFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
Profile profile = Profile.getCurrentProfile();
final String id = profile.getId();
final String userName = profile.getName();
final String firstName = profile.getFirstName();
SharedPreferences prefs = this.getActivity().getSharedPreferences("com.george.coffeeconversation", Context.MODE_PRIVATE);
TextView startTextView = (TextView)view.findViewById(R.id.start_text);
if (prefs.getBoolean("firstrun", true)) {
startTextView.setText(getResources().getString(R.string.lets_start));
prefs.edit().putBoolean("firstrun", false).apply();
}
else{
startTextView.setText(getResources().getString(R.string.welcome)+ " " + firstName);
}
return inflater.inflate(R.layout.tab_fragment_1, container, false);
}
}
推荐答案
你正在两次充气您的视图而不是返回您正在进行的工作以正确设置文本。
You are inflating your view twice instead of returning the one you did the work on to set the text correctly.
替换
return inflater.inflate(R.layout.tab_fragment_1, container, false);
使用
return view;
这篇关于从Fragment设置textView文本将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文