如何用自己的 java class 更改片段文本视图?

how change fragment textview with own java class?

Fragment的Activity中的TextView有3种情况我都试过改了,改不了TextView
是否有任何解决方案可以在自己的 Fragment_Activity 或 Main_Activity 中更改 Fragment 的 TextView?

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"
        />
....

调用片段 class 以在 MainActivity 中查看:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_weixin);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        instance = this;

...

 View view2 = mLi.inflate(R.layout.fragment_blank_fragment2, null);
           views.add(view2);
...
}

因为我们看不到整个 Fragment.xml 文件或您的 activity 主文件的 xml 我不能确定是什么问题,但看起来像你可能没有在 Fragment.xml 文件中指定片段 class,因此永远不会调用 oncreate。我建议你在 main_weixin xml 中添加 fragment 属性,并在你的 activity 中删除 view2 inflation 然后你可以简单地添加这个你的 main_weixin.xml:

<fragment
    android:name="your_package_name.Fragment"
    android:id="@+id/fragment_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

须知

  1. 在片段的 onCreate 中 getView() returns null 因为仍然未创建视图。
  2. 在片段中创建一个方法,使用片段实例为 activity 中的 TextView 设置值。

示例: 片段

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);
    }
}

Activity

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");

    }
}

访问官方文档Communicating with Other Fragments