45 Android activity 向 fragment 传值
16lz
2021-01-23
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
更多相关文章
- Android 加载图片文件 函数
- Android Java代码执行adb Shell命令
- android中listview分批加载数据
- 探寻 Android 代码抄袭细节,情节还不算严重
- android 简单的上拉加载实现
- Android清除本地数据缓存代码案例
- 【Android学习笔记】AutoCompleteTextView(数组在代码中)
- Android 高通代码预制apk可卸载,恢复出厂设置apk可恢复 Android M