1.布局文件
mian.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/linearLayout" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <EditText 8 android:id="@+id/editText1" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" /> 11 12 <Button 13 android:id="@+id/button1" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" /> 16 17 </LinearLayout>
fragment.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <TextView 7 android:id="@+id/textView1" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"/> 10 11 12 </LinearLayout>
2.java代码
MainActivity.class
1 public class MainActivity extends ActionBarActivity implements MyListener{ 2 3 private Button b1; 4 private EditText et1; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.main); 11 12 et1 = (EditText) findViewById( R.id.editText1); 13 b1 = (Button) findViewById(R.id.button1); 14 b1.setOnClickListener(new OnClickListener() { 15 16 @Override 17 public void onClick(View v) { 18 // TODO Auto-generated method stub 19 String text = et1.getText().toString(); 20 Bundle bundle = new Bundle(); 21 Myfragment myfragment = new Myfragment(); 22 bundle.putString("name", text); 23 myfragment.setArguments(bundle); 24 FragmentManager fragmentManager = getFragmentManager(); 25 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 26 fragmentTransaction.add(R.id.linearLayout, myfragment, "myfragment"); 27 fragmentTransaction.addToBackStack(null); 28 fragmentTransaction.commit(); 29 Toast.makeText(MainActivity.this, "向fragment发送数据"+text, Toast.LENGTH_SHORT).show(); 30 31 } 32 }); 33 } 34 35 @Override 36 public void thank(String code) { 37 // TODO Auto-generated method stub 38 Toast.makeText(MainActivity.this, "成功接收"+code+"客气", Toast.LENGTH_SHORT).show(); 39 } 40 41 42 }
Myfragment
1 public class Myfragment extends Fragment{ 2 3 private TextView tv1; 4 private String code = "Thanks"; 5 private MyListener listener; 6 7 public interface MyListener{ 8 9 public void thank(String code); 10 11 } 12 13 @Override 14 public void onAttach(Activity activity) { 15 // TODO Auto-generated method stub 16 listener = (MyListener) activity; 17 super.onAttach(activity); 18 } 19 20 @Override 21 public View onCreateView(LayoutInflater inflater, ViewGroup container, 22 Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 View view = inflater.inflate(R.layout.fragment, container,false); 25 tv1 = (TextView) view.findViewById(R.id.textView1); 26 String text = getArguments().get("name")+""; 27 tv1.setText(text); 28 Toast.makeText(getActivity(), "已经成功接收"+text, Toast.LENGTH_SHORT).show(); 29 Toast.makeText(getActivity(), "向Activity发送"+text, Toast.LENGTH_SHORT).show(); 30 listener.thank(code); 31 return view; 32 } 33 34 }
原文:http://www.cnblogs.com/ShawDd/p/4136084.html