【Android】22、碎片之間的通信

1、新建fragment1和fragment2.xml佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FF0">
    <TextView
        android:id="@+id/txt_fragment1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="碎片1"
        android:textSize="30sp"
        android:textColor="#F00" />

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#0F0">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="碎片2"
        android:textSize="30sp"
        android:textColor="#F0F" />
    <Button
        android:id="@+id/btn_gettext"
        android:text="獲取第一個碎片文字"
        android:onClick="onClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

2、新建fragment1.java和fragment2.java文件

public class Fragment1 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}
public class Fragment2 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        Button button = (Button) getActivity().findViewById(R.id.btn_gettext);
    button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {
            TextView textView = (TextView) getActivity().findViewById(R.id.txt_fragment1);
            Toast.makeText(getActivity(), textView.getText().toString(), Toast.LENGTH_LONG).show();
               }
         });
    }

3、修改main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <fragment
        android:id="@+id/fragment1"
        android:name="com.wxy.gettxtfromfragment.Fragment1"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"/>
    <fragment
        android:id="@+id/fragment2"
        android:name="com.wxy.gettxtfromfragment.Fragment2"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"/>

</LinearLayout>

4、實例說明:

因爲碎片是嵌入活動當中的,所以可以通過getActivity()的方法來獲取當前的活動,然後通過findViewById()方法來定位該碎片中包含的視圖。

5、另一種方法是,將fragment2.java改成和fragmen1.java類似的文件。其他代碼加在主activity.java中。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onClick(View view){
        TextView textView= (TextView) findViewById(R.id.txt_fragment1);
        Toast.makeText(this,textView.getText().toString(),Toast.LENGTH_LONG).show();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章