Fragment之间传值

来源:互联网 发布:淘宝v任务平台下载 编辑:程序博客网 时间:2024/06/05 21:02

FragmentValueActivity.java

package com.studio.fragmentdemo;import android.content.ReceiverCallNotAllowedException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class FragmentValueActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_fragment_value);        getSupportFragmentManager().beginTransaction().add(R.id.container,new FragmentValues1()).commit();    }}

通过getSupportFragmentManager().beginTransaction().add(R.id.container,new FragmentValues1()).commit();一行代码就实现了动态加载fragment。


activity_fragment_value.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="16dp"    android:paddingRight="16dp"    android:paddingTop="16dp"    android:paddingBottom="16dp"    tools:context="com.studio.fragmentdemo.FragmentValueActivity">    <FrameLayout        android:id="@+id/container"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <EditText        android:id="@+id/et"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="input" />    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="send"        android:text="send value" /></LinearLayout>

这里写图片描述


FragmentValues1.java

package com.studio.fragmentdemo;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;public class FragmentValues1 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment1, container, false);        final EditText editText = view.findViewById(R.id.et);        Button button = view.findViewById(R.id.btn1);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                FragmentValues2 fragmentValues2 = new FragmentValues2();                Bundle bundle = new Bundle();                bundle.putString("arg", editText.getText().toString());                fragmentValues2.setArguments(bundle);                getFragmentManager().beginTransaction().replace(R.id.container, fragmentValues2).commit();            }        });        return view;    }}

当点击按钮时触发点击事件,将输入框中的字符传递给bundle对象,再调用fragment的setArguments方法传入bundle对象,将数据传递给了另一个fragment。


FragmentValues2

package com.studio.fragmentdemo;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class FragmentValues2 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment2, container, false);        TextView textView = view.findViewById(R.id.tv);        textView.setText(getArguments().getString("arg"));        return view;    }}

通过getArguments方法获取到bundle对象再调用getString方法,通过键来找到传递过来的数据。


原创粉丝点击