标签:
MainActivity4代码(Activity-Fragment通信)
1 package fragmentdemo.example.administrator.fragmentdemo; 2 3 import android.app.Activity; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 /** 13 * Created by Administrator on 2016/5/7. 14 */ 15 public class MainActivity4 extends Activity { 16 private Button send; 17 private EditText editText; 18 19 protected void onCreate(Bundle savedInstanceState) { 20 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main4); 23 send= (Button) findViewById(R.id.send); 24 editText= (EditText) findViewById(R.id.editText); 25 send.setOnClickListener(new View.OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 String text=editText.getText().toString(); 29 MyFragment5 myFragment5=new MyFragment5();/*新建myFragment*/ 30 Bundle bundle=new Bundle();/*创建Bundle数据包*/ 31 bundle.putString("name",text); 32 myFragment5.setArguments(bundle);/*调用setArguments(bundle)方法*/ 33 FragmentManager fragmentManager = getFragmentManager();/*获得FragmentManager管理者*/ 34 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();/*开启一个事务*/ 35 fragmentTransaction.add(R.id.layout,myFragment5,"myFragment5"); 36 fragmentTransaction.commit();/*提交事务*/ 37 Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show(); 38 } 39 }); 40 } 41 }
Main方法
1 package fragmentdemo.example.administrator.fragmentdemo; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import android.app.Activity; 8 import android.app.Fragment; 9 import android.app.FragmentManager; 10 import android.app.FragmentTransaction; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.widget.RadioGroup; 14 import android.widget.RadioGroup.OnCheckedChangeListener; 15 /*1)Fragment可以作为Activity界面的一部分组成出现; 16 (2)可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用; 17 (3)在Activity运行过程中,可以添加、移除或替换Fragment; 18 (4)Fragment可以响应自己的输入事件,并且有自己的声明周期,它们的生命周期受宿主Activity的生命周期影响; 19 (5)Fragment在第一次绘制它的用户界面时,系统会调用onCreateView()方法,此方法返回一个View。(如果不显示UI,返回null); 20 Fragment两种加载方式:静态加载、动态加载。*/ 21 22 public class MainActivity extends Activity implements OnCheckedChangeListener 23 { 24 25 private RadioGroup group; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 // TODO Auto-generated method stub 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 group = (RadioGroup) findViewById(R.id.radiogroup); 33 group.setOnCheckedChangeListener(this); 34 35 } 36 37 @Override 38 public void onCheckedChanged(RadioGroup group, int checkedId) { 39 // TODO Auto-generated method stub 40 41 switch (checkedId) { 42 case R.id.first: { 43 Intent intent=new Intent(this,MainActivity2.class); 44 startActivity(intent); 45 break; 46 47 } 48 case R.id.second: { 49 /*Fragment 动态加载*/ 50 MyFragment2 myFragment2=new MyFragment2();/*创建实例*/ 51 FragmentManager fragmentManager = getFragmentManager();/*获取到FragmentManager*/ 52 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();/*开启事务*/ 53 fragmentTransaction.add(R.id.frame,myFragment2); 54 fragmentTransaction.addToBackStack(null);/*通过物理返回键返回*/ 55 fragmentTransaction.commit();/*提交事务*/ 56 57 58 break; 59 } 60 case R.id.thrid: { 61 Intent intent=new Intent(this,MainActivity3.class); 62 startActivity(intent); 63 break; 64 65 66 } 67 case R.id.fourth: { 68 Intent intent=new Intent(this,MainActivity4.class); 69 startActivity(intent); 70 break; 71 72 73 } 74 } 75 }
MyFragment5(Fragment-Activity通信)
package fragmentdemo.example.administrator.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; /** * Created by Administrator on 2016/5/7. */ public class MyFragment5 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, container, false); TextView tv = (TextView) view.findViewById(R.id.text); String text=getArguments().get("name")+""; tv.setText(text); Toast.makeText(getActivity(),"已成功接收到"+text,Toast.LENGTH_SHORT).show(); return view; } }
main4.xml代码(LinearLayout代码)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:id="@+id/layout"> 6 <EditText 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:id="@+id/editText"/> 10 <Button 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="发送" 14 android:id="@+id/send"/> 15 </LinearLayout>
在AndroidManifest中进行注册
<activity android:name=".MainActivity4"></activity>
标签:
原文地址:http://www.cnblogs.com/633sylss/p/5468018.html