如何使用自己的java类更改片段textview?
在三种情况下,我试图在片段的活动中更改TextView,但无法更改TextView。如何使用自己的java类更改片段textview?,java,android,android-fragments,android-activity,fragment,Java,Android,Android Fragments,Android Activity,Fragment,在三种情况下,我试图在片段的活动中更改TextView,但无法更改TextView。 在自己的Fragment_活动或Main_活动中,是否有更改Fragment文本视图的解决方案 Fragment.java @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) {
在自己的Fragment_活动或Main_活动中,是否有更改Fragment文本视图的解决方案 Fragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
TextView textView = (TextView) getView().findViewById(R.id.textView1);
textView.setText("Done!");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("Done!");
return view;
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView tv = (TextView) getView().findViewById(R.id.textView1);
tv.setText("Done!");
}
Fragment.xml
...
<TextView
android:textColor="@color/myBlack"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
....
因为我们看不到整个Fragment.xml文件或您主要活动的xml,所以我无法确定问题出在哪里,但似乎您可能没有在Fragment.xml文件中指定Fragment类,因此永远不会调用oncreate。我建议您在
main\u weixin
xml中添加fragment属性,并删除主活动中的view2
膨胀,然后您只需将其添加到main\u weixin.xml
:
<fragment
android:name="your_package_name.Fragment"
android:id="@+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
需要知道的事情
getView()
返回null,因为尚未创建中的视图TextView
设置值public class MyActivity extends FragmentActivity {
Fragment1 fragment1;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_fragment);
//as you are using static fragment so create fragment instance using findFragmentById(id)
fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragment1.setViewText("Message");
}
}
访问官方文档
但您在MainActivity中添加片段的位置,或者是静态片段。我创建了静态片段,并添加到MainActivity[link](http:\\yekvip.com\MyApp940205.zip)的LinearLayout中。我尝试了它,但什么都没有。您能看看示例吗?[链接](http:\\yekvip.com\MyApp940205.zip)这花了我10天的时间:请您发布完整的xml和java代码,以便我们可以提供帮助[链接](http:\\yekvip.com\MyApp940205.zip)“BlankFragment2”是我的片段,“main_weixin”是我的主要活动,我想学习和做的只是运行应用程序并单击“mona”选项卡,更改“文本视图”
public class Fragment1 extends Fragment {
TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("Done!");
return view;
}
public void setViewText(String text){
tv.setText(text);
}
}
public class MyActivity extends FragmentActivity {
Fragment1 fragment1;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_fragment);
//as you are using static fragment so create fragment instance using findFragmentById(id)
fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragment1.setViewText("Message");
}
}