标签:
Activity:
package com.itheima.senddata; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private Fragment03 fg3; private EditText et_main; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main = (EditText) findViewById(R.id.et);//Activity的文本控件 fg3 = new Fragment03(); //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg3); //提交 ft.commit(); } public void click1(View v){ //把fragment01的界面显示至帧布局中 //创建fragment对象 Fragment01 fg1 = new Fragment01(); //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg1); //提交 ft.commit(); } public void click2(View v){ //把fragment01的界面显示至帧布局中 //创建fragment对象 Fragment02 fg2 = new Fragment02(); //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg2); //提交 ft.commit(); } public void click3(View v){ //把fragment01的界面显示至帧布局中 //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg3); //提交 ft.commit(); } public void click4(View v){//Activity的Button控件 String text = et_main.getText().toString(); //传递数据,setText是fg3的方法 fg3.setText(text); } public void setText(String text){ et_main.setText(text); } }
fragment01:
package com.itheima.senddata; 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; public class Fragment01 extends Fragment { //返回的view对象会作为fragment01的内容显示在屏幕上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment01, null); final EditText et = (EditText) v.findViewById(R.id.et);//Fragment01的控件 Button bt = (Button) v.findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { //Fragment不是在context,所以click用监听,不直接写函数,因为直接写函数是在Activity中的。 @Override public void onClick(View v) { String text = et.getText().toString(); //把数据传递给activity,getActivity()拿到与Fragment关联的Activity,setText是Activity的方法 ((MainActivity)getActivity()).setText(text); } }); return v; } }
fragment03:
package com.itheima.senddata; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment03 extends Fragment { private TextView tv; //返回的view对象会作为fragment03的内容显示在屏幕上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.fragment03, null); tv = (TextView) v.findViewById(R.id.tv);//Fragment03的控件 return v; } public void setText(String text){ tv.setText(text); } }
android78 Fragment和Activity 传递数据
标签:
原文地址:http://www.cnblogs.com/yaowen/p/4932051.html