Activity向静态加载的Fragment进行传值的思路:
1,在Fragment中定义好 get() 和 set(..) 方法,
2,通过 FragmentManager的 findFragmentById(..) 或者是 findFragmentByTag(..)得到加载进来的Fragment
3,在Activity中调用fragment的 set(..) 方法传值过去,然后在fragment中调用 get() 获取值
2,通过 FragmentManager的 findFragmentById(..) 或者是 findFragmentByTag(..)得到加载进来的Fragment
3,在Activity中调用fragment的 set(..) 方法传值过去,然后在fragment中调用 get() 获取值
注意:
---- 这里的关键是要用 FragmentManager的 findFragmentById(..) 或者是 findFragmentByTag(..) 得到加载进来的Fragment.
下面直接上代码:
MainActivity中的代码是 :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 从Activity向静态加载的Fragment中传值的方法是:
* 1,在Fragment中定义好 get() 和 set(..) 方法,
* 2,通过 FragmentManager的 findFragmentById(..) 或者是 findFragmentByTag(..)得到加载进来的Fragment
* 3,在Activity中调用fragment的 set(..) 方法传值过去,然后在fragment中调用 get() 获取值
*
* 注意 :
* 这里的关键是要用 FragmentManager的 findFragmentById(..) 或者是 findFragmentByTag(..) 得到加载进来
* 的Fragment.
*/
FragmentManager fragmentManager = getFragmentManager();
MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fragment);
String text = "这是传递过去的值: 我爱娜娜!";
myFragment.setText(text);//调用fragment的set()来传值过去
}
}
布局文件 activity_main.xml 中的代码:
<RelativeLayout 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="com.xulunpeng.dongtaidown.MainActivity">
<fragment
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:id="@+id/fragment"
android:name="com.xulunpeng.tongxinfat.MyFragment"/>
</RelativeLayout>
MyFragment中的代码:
public class MyFragment extends Fragment {
//定义好 get() 和 set(..)方法用来传值 取值
String text = "";
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layou_fragment, null);
final TextView textView = (TextView) view.findViewById(R.id.textView);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String text = getText();//获取传递过来的值
textView.setText(text);
}
});
return view;
}
}
<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="vertical" >
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@android:color/black"
android:layout_gravity="center">
<TextView
android:layout_width="180dp"
android:layout_height="40dp"
android:background="@android:color/white"
android:layout_centerInParent="true"
android:textSize="20sp"
android:id="@+id/textView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="点击按钮获取值"
android:id="@+id/btn"/>
</RelativeLayout>
</LinearLayout>
点击传值前:
点击传值后: