如何在另一个片段的片段中设置文本按钮

时间:2017-03-23 03:13:18

标签: android xml android-layout android-fragments

我有片段问题。

在我的活动中,我调用Fragment1

- >在Fragment1中,我调用Fragment2

- >在Fragment2中,当我点击一个按钮时,Fragment1中的按钮文本将被更改。(我不知道该怎么做)

这是我的源代码:

activity_main.xml中

<Button
    android:id="@+id/btnFragment1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Call Fragment 1" />

<LinearLayout
    android:id="@+id/lnFragment1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>

fragment1.xml:

<Button
    android:id="@+id/btnFragment2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Call Fragment 2" />

<LinearLayout
    android:id="@+id/lnFragment2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>

fragment2.xml:

<Button
    android:id="@+id/btnChange"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:text="Change text for button in fragment 1 to 'DONE'" />

<LinearLayout
    android:id="@+id/lnFragment2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnFragment1 = (Button) findViewById(R.id.btnFragment1);

    btnFragment1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = new Fragment1();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //lay fragmenttransaction thong qua fragment manager
            fragmentTransaction.replace(R.id.lnFragment1,fragment);
            fragmentTransaction.commit();
        }
    });

}

Fragment1.java:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1,container,false);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    Button btnFragment2 = (Button) getView().findViewById(R.id.btnFragment2);

    btnFragment2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = new Fragment2();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //lay fragmenttransaction thong qua fragment manager
            fragmentTransaction.replace(R.id.lnFragment2,fragment);
            fragmentTransaction.commit();
        }
    });
}

Fragment2.java:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment2,container,false);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    final Button btnChange = (Button) getView().findViewById(R.id.btnChange);
    btnChange.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment1 = new Fragment1();
            View vFragment1 = fragment1.getView();
            Button btnChange = (Button) vFragment1.findViewById(R.id.btnChange);
            btnChange.setText("Done");
        }
    });
}

谢谢!

3 个答案:

答案 0 :(得分:0)

创建一个Contract接口并在片段中实现它们之间的通信,片段不能像这样直接通信。

答案 1 :(得分:0)

另一种实现此目的的方法是广播管理器

Framgent -1

在你的Framgent -1

private MyBroadRequestReceiver receiver;
onCreateView

中的

IntentFilter filter = new IntentFilter(ACTION_NAME);
 receiver = new MyBroadRequestReceiver();
registerReceiver( receiver, filter);

公共类MyBroadRequestReceiver扩展了BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

     //update your Button text here

    }


}

@Override
    public void onDestroy() {
        this.unregisterReceiver(receiver);
        super.onDestroy();
    }

<强>片段-2

从Fragment-2中调用BroadCast 什么时候想要更新第一个片段按钮的文本

Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(ACTION_NAME);       
        sendBroadcast(broadcastIntent);

确定两个片段的确定动作名称应该相同。

答案 2 :(得分:0)

不要承受太大的压力。有一个图书馆,可以让您的工作更轻松。 EventBus。你也可以在Fragment Refresh

找到好的教程