安卓 Fragment进阶

fragment传值

activity向fragment传值

fragment的xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="等待" />

</FrameLayout>

fragment的java文件

public class BlankFragment2 extends Fragment {
    private TextView fra;
    public BlankFragment2() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
        //拿控件
        fra = (TextView) inflate.findViewById(R.id.fra);
        //给控件赋值
        Bundle bundle = getArguments();
        if(bundle != null){
            String ket = bundle.getString("ket");
            fra.setText(ket);
        }
        return inflate;
    }
}

activity的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et"
        />
    <Button
        android:text="传值"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="aaa"
        />
    <LinearLayout
        android:id="@+id/fang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    </LinearLayout>
</LinearLayout>

activity的java文件

public class Main2Activity extends AppCompatActivity {

    private FragmentManager manager;
    private FragmentTransaction fragmentTransaction;
    private EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        et = (EditText) findViewById(R.id.et);
        //管理者
        manager = getSupportFragmentManager();
        //开启事务
        fragmentTransaction = manager.beginTransaction();
        BlankFragment2 blankFragment2 = new BlankFragment2();
        fragmentTransaction.add(R.id.fang,blankFragment2);
        //提交事务
        fragmentTransaction.commit();
    }

    public void aaa(View view) {
        //获取输入的值
        String s = et.getText().toString();
        //创建fragment对象
        BlankFragment2 blankFragment2 = new BlankFragment2();
        //创建bundle
        Bundle bundle = new Bundle();
        bundle.putString("key",s);
        //给fragment赋值
        blankFragment2.setArguments(bundle);
        //动态修改
        manager = getSupportFragmentManager();
        fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.fang,blankFragment2);
        fragmentTransaction.commit();
    }
}

fragment向activity传值

fragment的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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment3"
    android:orientation="vertical"
    >

    <!-- TODO: Update blank fragment layout -->
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        />
</LinearLayout>

fragment的java文件

public class BlankFragment3 extends Fragment {

    private EditText et;
    private Button bt;
    private MyListener listener;

    public BlankFragment3() {
        // Required empty public constructor
    }

    //自定义接口
    public interface MyListener{
        void message(String s);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //拿到和当前碎片相关联的activity
        listener = (MyListener) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_blank_fragment3, container, false);
        //取到所有的控件
        et = (EditText) inflate.findViewById(R.id.et);
        bt = (Button) inflate.findViewById(R.id.bt);
        //点击事件
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = et.getText().toString();
                if (s != null){
                    //父类引用指向子类对象
                    listener.message(s);
                }
            }
        });
        return inflate;
    }
}

activity的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main3Activity"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fras"
        android:name="com.example.day05exe.BlankFragment3">

    </fragment>
</LinearLayout>

activity的java文件

public class Main3Activity extends AppCompatActivity implements BlankFragment3.MyListener{

    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        tv = (TextView) findViewById(R.id.tv);
    }
    @Override
    public void message(String s) {
        tv.setText(s);
    }
}

fragment向fragment传值

创建两个碎片放到activity中
第一个碎片的布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment1"
    android:orientation="vertical"
    >

    <!-- TODO: Update blank fragment layout -->
    <EditText
        android:id="@+id/ets"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        />

</LinearLayout>

第二个碎片的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fadaozhe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

第一个碎片的java文件

public class Fragment1 extends Fragment {

    private EditText ets;
    private Button bts;

    public Fragment1() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_fragment1, container, false);

        ets = (EditText) inflate.findViewById(R.id.ets);
        bts = (Button) inflate.findViewById(R.id.bts);

        bts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //要传的值
                String s = ets.getText().toString();
                //通过id找的指定fragment,得到他所有的view.在通过findViewById 找到组件.赋值
                TextView textView = getFragmentManager().findFragmentById(R.id.fram2).getView().findViewById(R.id.fadaozhe);
                textView.setText(s);
            }
        });
        return inflate;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值