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方法,通过键来找到传递过来的数据。
阅读全文
0 0
- Fragment之间传值
- Fragment之间传值
- Fragment之间传值
- Fragment 之间传值
- Fragment 之间的传值
- activity嵌套fragment,fragment内嵌fragment之间传值切换fragment
- Activity和Fragment之间的传值
- Fragment与Activity之间传值
- Android fragment 之间传值的方法
- android中fragment之间传值
- Activity与Fragment之间的传值
- Fragment之间传值的方法
- DialogFragment与Fragment之间互相传值
- Fragment之间传值CallBack(Fragment01)
- Fragment之间传值CallBack(Fragment02)
- Fragment之间传值CallBack(MainActivity)
- Activity与Fragment之间的传值
- Fragment与Activity之间传值
- 书
- 线程方面
- struts2注解返回json串实现方式(序列化对象属性输出)
- ifconfig,netstat 命令找不到解決办法
- C++异常
- Fragment之间传值
- Mysql语句,用到case when ,concat
- less基础讲解
- 2017.08.07小结
- Composer
- 数据库连接池Datasource
- 实现布隆过滤器
- 第一篇 人类存在的意义
- 动作识别:improved dense trajectories(iDT)特征编码—Fisher Vector代码学习