left.xml (加载到fragment)

<?xml version="1.0" encoding="utf-8"?>    

right.xml (加载到fragment)

<?xml version="1.0" encoding="utf-8"?>    



activity_main.xml

                


MianActivity.java 代码

package com.example.android_fragment_activity;import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;public class MainActivity extends Activity {private FragmentManager manager;private FragmentTransaction transaction ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager=getFragmentManager();transaction=manager.beginTransaction();LeftFragment leftFragment=new LeftFragment();Bundle bundle=new Bundle();bundle.putInt("id", 10010);leftFragment.setArguments(bundle);RightFragment rightFragment=new RightFragment();Bundle bundle2=new Bundle();bundle2.putString("name", "Jack");rightFragment.setArguments(bundle2);transaction.add(R.id.left, leftFragment, "leftFragment");transaction.add(R.id.right, rightFragment, "rightFragment");transaction.commit();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

LeftFragment extends fragment


package com.example.android_fragment_activity;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 LeftFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view =inflater.inflate(R.layout.left, null);TextView textview=(TextView)view.findViewById(R.id.textViewleft);Bundle bundle=getArguments();int id=bundle.getInt("id");textview.setText(""+id);return view;}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}}

RightFragment extends Fragment

package com.example.android_fragment_activity;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 RightFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view =inflater.inflate(R.layout.right, null);TextView textView=(TextView)view.findViewById(R.id.textViewright);Bundle bundle=getArguments();String str=bundle.getString("name");textView.setText(str);return view;}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}}


左边显示10010   右边显示Jack




45 Android activity 向 fragment 传值_第1张图片





更多相关文章

  1. Android 加载图片文件 函数
  2. Android Java代码执行adb Shell命令
  3. android中listview分批加载数据
  4. 探寻 Android 代码抄袭细节,情节还不算严重
  5. android 简单的上拉加载实现
  6. Android清除本地数据缓存代码案例
  7. 【Android学习笔记】AutoCompleteTextView(数组在代码中)
  8. Android 高通代码预制apk可卸载,恢复出厂设置apk可恢复 Android M

随机推荐

  1. android一个简单的线程实例
  2. android第三天--AbsoluteLayout绝对布局
  3. Android控件之——输入框TextInputLayout
  4. 像素鸟(flapybird)的简单实现
  5. android面试题(1)
  6. [Android]获取网络连接状态
  7. Android使用selector改变文字的颜色
  8. android中ProgressBar的使用SeekBar的使
  9. Android 可選文件格式瀏覽器
  10. Android xposed Hook 初探01