静态fragment传值

第一种:

主布局文件:

  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal">  
  6.     <fragment   
  7.         android:id="@+id/fg1"  
  8.         android:name="mars.day12_fragmenttofragment.Fragment1"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_weight="1"  
  11.         android:layout_width="0dp"/>  
  12.     <LinearLayout  
  13.         android:id="@+id/fg2"  
  14.         android:layout_height="match_parent"  
  15.         android:layout_weight="1"   
  16.         android:layout_width="0dp"  
  17.         android:orientation="horizontal">  
  18.     </LinearLayout>  
  19.   
  20. </LinearLayout>  
  21. </span>  
第一个Fragment的布局文件
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="#00ff00"  
  7.     android:gravity="center">  
  8.     <EditText   
  9.         android:id="@+id/et"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="30sp"/>  
  13.     <Button   
  14.         android:id="@+id/bt"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="发送"  
  18.         android:textSize="30sp"/>  
  19.   
  20. </LinearLayout>  
  21. </span>  
第2个Fragment的布局文件
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="#0000ff"  
  7.     android:gravity="center">  
  8.     <TextView   
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:textSize="30sp"  
  12.         android:text="接收的内容:"/>  
  13.     <TextView   
  14.         android:id="@+id/tv"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:textSize="30sp"/>  
  18.   
  19. </LinearLayout>  
  20. </span>  
第1个Fragment的逻辑代码文件
  1. <span style="font-size:18px;">package mars.day12_fragmenttofragment;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.app.Fragment;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13.   
  14. @SuppressLint("NewApi"public class Fragment1 extends Fragment{  
  15.     EditText et;  
  16.     CallBack call;  
  17.     @Override  
  18.     public void onAttach(Activity activity) {  
  19.         super.onAttach(activity);  
  20.         call=(CallBack) getActivity();  
  21.     }  
  22.     @Override  
  23.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  24.             Bundle savedInstanceState) {  
  25.         View view=inflater.inflate(R.layout.fragment1,container,false);  
  26.         et=(EditText) view.findViewById(R.id.et);  
  27.         Button bt=(Button) view.findViewById(R.id.bt);  
  28.         bt.setOnClickListener(new OnClickListener() {  
  29.               
  30.             @Override  
  31.             public void onClick(View v) {  
  32.             call.getData(et.getText().toString());    
  33.             }  
  34.         });  
  35.         return view;  
  36.     }  
  37.     public interface CallBack  
  38.     {  
  39.         public void getData(String data);  
  40.     }  
  41. }  
  42. </span>  

主逻辑代码文件:

  1. <span style="font-size:18px;">package mars.day12_fragmenttofragment;  
  2.   
  3. import mars.day12_fragmenttofragment.Fragment1.CallBack;  
  4. import android.os.Bundle;  
  5. import android.annotation.SuppressLint;  
  6. import android.app.Activity;  
  7. import android.app.FragmentManager;  
  8. import android.app.FragmentTransaction;  
  9. import android.view.Menu;  
  10.   
  11. public class MainActivity extends Activity implements CallBack{  
  12.   
  13.     FragmentManager fm;  
  14.     @SuppressLint("NewApi"@Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         fm=getFragmentManager();  
  19.         FragmentTransaction shiwu = fm.beginTransaction();  
  20.         shiwu.replace(R.id.fg2,new Fragment2());  
  21.         shiwu.commit();  
  22.     }  
  23.   
  24.     @SuppressLint("NewApi"@Override  
  25.     public void getData(String data) {  
  26.         Fragment2 fg2=new Fragment2();  
  27.         Bundle bundle=new Bundle();  
  28.         bundle.putString("mes",data);  
  29.         fg2.setArguments(bundle);  
  30.         FragmentTransaction shiwu = fm.beginTransaction();  
  31.         shiwu.replace(R.id.fg2,fg2);  
  32.         shiwu.commit();  
  33.     }  
  34.       
  35.   
  36. }  
  37. </span>  

第二个Fragment的逻辑代码文件
  1. <span style="font-size:18px;">package mars.day12_fragmenttofragment;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Fragment;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.TextView;  
  10.   
  11. @SuppressLint("NewApi"public class Fragment2 extends Fragment{  
  12.   
  13.     @Override  
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  15.             Bundle savedInstanceState) {  
  16.         View view=inflater.inflate(R.layout.fragment2,container,false);  
  17.         TextView tv=(TextView) view.findViewById(R.id.tv);  
  18.         Bundle bundle=getArguments();  
  19.         //判断需写  
  20.         if(bundle!=null)  
  21.         {  
  22.             tv.setText(bundle.getString("mes"));  
  23.         }  
  24.         return view;  
  25.     }  
  26. }  
  27. </span>  


第二种和第三种方法:

Fragment对应的布局文件两个都同上


主布局文件

  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal">  
  6.     <fragment   
  7.         android:id="@+id/fg1"  
  8.         android:name="mars.day12_fragmenttofragment.Fragment1"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_weight="1"  
  11.         android:layout_width="0dp"/>  
  12.     <LinearLayout  
  13.         android:id="@+id/fg2"  
  14.         android:layout_height="match_parent"  
  15.         android:layout_weight="1"   
  16.         android:layout_width="0dp"  
  17.         android:orientation="horizontal">  
  18.     </LinearLayout>  
  19.   
  20. </LinearLayout>  
  21. </span>  

第一个Fragment的逻辑代码文件
  1. <span style="font-size:18px;">package mars.method2and3;  
  2.   
  3. import mars.day12_fragmenttofragment.R;  
  4. import android.annotation.SuppressLint;  
  5. import android.app.Activity;  
  6. import android.app.Fragment;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.ViewGroup;  
  12. import android.widget.Button;  
  13. import android.widget.EditText;  
  14. import android.widget.TextView;  
  15.   
  16. @SuppressLint("NewApi"public class Fragment1 extends Fragment{  
  17.     EditText et;  
  18.       
  19.     @Override  
  20.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  21.             Bundle savedInstanceState) {  
  22.         View view=inflater.inflate(R.layout.fragment1,container,false);  
  23.         et=(EditText) view.findViewById(R.id.et);  
  24.         Button bt=(Button) view.findViewById(R.id.bt);  
  25.         bt.setOnClickListener(new OnClickListener() {  
  26.               
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 String data=et.getText().toString();  
  30.                 //第二种方法  
  31. //                Fragment2 fg2=(Fragment2) getFragmentManager().findFragmentById(R.id.fg2);  
  32. //                fg2.setData(data);  
  33.                 //第三种方法  
  34.                 TextView tv=(TextView) getActivity().findViewById(R.id.tv);  
  35.                 tv.setText(data);  
  36.             }  
  37.         });  
  38.         return view;  
  39.     }  
  40.   
  41. }</span>  

第2个Fragment的逻辑代码文件

  1. <span style="font-size:18px;">package mars.method2and3;  
  2.   
  3. import mars.day12_fragmenttofragment.R;  
  4. import android.annotation.SuppressLint;  
  5. import android.app.Fragment;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.TextView;  
  11.   
  12. @SuppressLint("NewApi"public class Fragment2 extends Fragment{  
  13.   
  14.     TextView tv;  
  15.     @Override  
  16.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  17.             Bundle savedInstanceState) {  
  18.         View view=inflater.inflate(R.layout.fragment2,container,false);  
  19.         tv=(TextView) view.findViewById(R.id.tv);  
  20.         return view;  
  21.           
  22.     }  
  23.     public void setData(String str)  
  24.     {  
  25.         tv.setText(str);  
  26.     }  
  27. }  
  28. </span> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值