14 fragment传值
两个fragment传值
方式一
布局文件代码:
<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"
android:orientation="horizontal"
tools:context=".MainActivity" > <!-- 静态 必须要写ID--> <fragment
android:id="@+id/fragment1"
android:name="com.qf.day14_fragment_4.fragment.MyFragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0f0" />
<!-- 动态 --> <LinearLayout
android:id="@+id/ll_fragment_id"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" /> </LinearLayout>布局文件逻辑代码:
package com.qf.day14_fragment_4; import com.qf.day14_fragment_4.fragment.MyFragment2; import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //管理者对象
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); transaction.replace(R.id.ll_fragment_id,myFragment2); transaction.commit();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
其中一个fragment逻辑代码:
package com.qf.day14_fragment_4.fragment; import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button; import com.qf.day14_fragment_4.R; public class MyFragment1 extends Fragment{ private Button btn; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment1_layout, container, false); btn = (Button) v.findViewById(R.id.btn);
//点击按钮传值
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub //管理者对象
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); Bundle bundle = new Bundle(); bundle.putString("msg", "左边Fragment向右边Fragment传值"); myFragment2.setArguments(bundle); transaction.replace(R.id.ll_fragment_id, myFragment2); transaction.commit();
}
});
return v;
}
}
方式二 前提:必须两个静态fragment
界面xml代码:
<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"
android:orientation="horizontal"
tools:context=".MainActivity" > <fragment
android:id="@+id/leftfragment"
android:name="com.qf.day14_fragment_demo5.fragment.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#00f" /> <fragment
android:id="@+id/rightfragment"
android:name="com.qf.day14_fragment_demo5.fragment.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>其中核心fragment代码
package com.qf.day14_fragment_demo5.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import com.qf.day14_fragment_demo5.R; public class Fragment1 extends Fragment{ private EditText etContent;
private Button btnSend; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub View view = inflater.inflate(R.layout.layout01, container, false);
etContent = (EditText) view.findViewById(R.id.et_content);
btnSend = (Button) view.findViewById(R.id.btn_send); btnSend.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub String msg = etContent.getText().toString().trim(); //通过Fragment的id 获取Fragment2的对象
// Fragment2 fragment2 = (Fragment2) getFragmentManager().findFragmentById(R.id.rightfragment);
// fragment2.setTextValues(msg); //TextView tv = (TextView) getFragmentManager().findFragmentById(R.id.rightfragment).getView().findViewById(R.id.tv_show);
TextView tv = (TextView) getActivity().findViewById(R.id.tv_show);
tv.setText(msg); } }); return view;
} }
fragment给界面传值(Activity) 接口回调
界面代码
package com.qf.day14_fragment_demo3; import com.qf.day14_fragment_demo3.callback1.CallBackValue; import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity implements CallBackValue{ private TextView tv; @SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.ll_fragment_id, new MyFragment());
transaction.commit();
} //回调接口的方法
@Override
public void sendMessage(String msg) {
// TODO Auto-generated method stub
tv.setText(msg);
} }
接口代码
package com.qf.day14_fragment_demo3.callback1; public interface CallBackValue { public void sendMessage(String msg); }
fragment代码
package com.qf.day14_fragment_demo3; import com.qf.day14_fragment_demo3.callback1.CallBackValue; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText; @SuppressLint("NewApi")
public class MyFragment extends Fragment{ CallBackValue callBackValue; private EditText et;
private Button btnFTA; /**
* 与Activity第一次链接是调用
*/
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity); //Fragment归属的Activity是getActivity
callBackValue = (CallBackValue) getActivity();
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_layout, container,false);
et = (EditText) v.findViewById(R.id.et);
btnFTA = (Button) v.findViewById(R.id.btn_FTA); btnFTA.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//将值传给Activity
String msg = et.getText().toString().trim(); callBackValue.sendMessage(msg);
}
}); return v;
} }
fragment 获取 界面数据(Activity)
- 可以fragment中直接getActivity 或者getContext 获取其对象
- 或者动态创建时用setAgunment 在fragment中getAgument
14 fragment传值的更多相关文章
- 14 fragment 创建
静态展示 注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点 步骤一:创建一个类继承 Fragment 代码类型一: package com.fmy.demo1; im ...
- 14 Fragment 碎片总结
Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...
- Android FragmentActivity 给Fragment传值
1.Fragment给Activity传值 定义一个在fragment 中 定义interface 监听器,让activity实现,并将activity的引用传递给fragment.即setListe ...
- 【android】activity、fragment传值例子
1:Activity篇 1.1向Activity传值 关键点在于putExtra.如果传递类的话,记得类实现Serializable接口 Intent intent = new Intent(Firs ...
- Activity向Fragment传值
发送数据 //Activity传值,通过Bundle Bundle bundle = new Bundle(); bundle.putString("MainActivity", ...
- 【Android】安卓开发之activity如何传值到fragment,activity与fragment传值
作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 大家知道,我们利用activity使 ...
- 两个activity或者activity和fragment传值
使用Fragment的时候可能需要在两个Fragment之间进行参数的传递,开始想着可以使用SharedPreferences进行处理,想想这些简单的参数没有必要使用这么麻烦的方式去实现,翻了一下Fr ...
- 14 Fragment的V4包的使用
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- 14 Fragment 注意点
API 过时问题 API 23过时 public void onAttach(Activity activity)替换为public void onAttach(Context context) 注意 ...
随机推荐
- NOIP 2014
Prob.1 生活大爆炸版 石头剪刀布 模拟.代码: #include<cstdio> #include<cstring> #include<iostream> u ...
- 【bzoj4570 scoi2016】妖怪
题目描述 邱老师是妖怪爱好者,他有n只妖怪,每只妖怪有攻击力atk和防御力dnf两种属性.邱老师立志成为妖怪大师,于是他从真新镇出发,踏上未知的旅途,见识不同的风景. 环境对妖怪的战斗力有很大影响,在 ...
- hdu 5591 BestCoder Round #65(博弈)
题意: 问题描述 ZYBZYB在远足中,和同学们玩了一个“数字炸弹”游戏:由主持人心里想一个在[1,N][1,N]中的数字XX,然后玩家们轮流猜一个数字,如果一个玩家恰好猜中XX则算负,否则主持人将告 ...
- ubuntu 安装 WPS for Linux(ubuntu)字体配置(字体缺失解决办法)及卸载libreoffice
从官网下载安装wps for Linux sudo dpkg -i wps-office_10.1.0.5672~a21_amd64.deb 启动WPS for Linux后,出现提示"系统 ...
- 00-Unit_Common综述-RecyclerView封装
自学安卓也有一年的时间了,与代码相伴的日子里,苦乐共存.能坚持到现在确实已见到了"往日所未曾见证的风采".今2018年4月2日,决定用一个案例:Unit_Common,把安卓基础的 ...
- jsvascript === 和==的区别
== 用于比较 判断 两者相等 ==在比较的时候可以转自动换数据类型 ===用于严格比较 判断两者严格相等 ===严格比较,不会进行自动转换,要求进行比较的操作数必须类型 ...
- Missing URI template variable 'XXXX' for method parameter of type String
原因:就是spring的controller上的@RequestMapping的实参和方法里面的形参名字不一致 方法:改成一样就可. ps.还能用绑定的方法,不建议,因为太麻烦了 @RequestMa ...
- Tomcat关闭日志输出
tomcat中禁用catalina.out的输出,又可能很大. 1.直接修改catalina.sh文件的输出语句. 在文件中找到以下内容. if [ -z "$CATALINA_OUT&qu ...
- SQL注入原理及绕过安全狗
1.什么是SQL注入攻击 SQL注入攻击指的是通过构造特殊的输入作为参数插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令 http://www.xxx.com/list. ...
- 下篇:python的基本数据类型以及对应的常用方法(列表、元组、字典、集合)
为了日后便于查询,本文所涉及到的所有命令集合如下: python中的基本数据类型有数字.字符串.布尔值.列表.元组.字典.就像每一个职业有自己特定的技能,比如医生能看病,农民能种田,每种数据类型也有属 ...