用actionBar和fragment做的Tab切换,怎么在第二个fragment里得到另一个fragment中edittext的值?
public class MainActivity extends Activity {
ActionBar.Tab tab1, tab2, tab3;
Fragment fragmentTab1 = new Tab_Conf();
Fragment fragmentTab2 = new Tab_Results();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = actionBar.newTab().setText("1");
tab2 = actionBar.newTab().setText("2");
tab3 = actionBar.newTab().setText("3");
tab1.setTabListener(new MyTabListener(fragmentTab1));
tab2.setTabListener(new MyTabListener(fragmentTab2));
tab3.setTabListener(new MyTabListener(fragmentTab3));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
}
public class MyTabListener implements ActionBar.TabListener {
Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// nothing done here
}
}
这是第一个fragment
public class Tab_Conf extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.tab_conf, container, false);
EditText txt_long;
txt_long = (EditText) view.findViewById(R.id.txt_longitude);
EditText txt_lati;
txt_lati = (EditText) view.findViewById(R.id.txt_latitude);
EditText txt_radius;
txt_radius = (EditText) view.findViewById(R.id.txt_radius);
txt_long.setText("45");
txt_lati.setText("-79");
txt_radius.setText("100");
return view;
}
}
这是第二个fragment,得到的txt_long 是空
public class Tab_Results extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_results, container, false);
EditText txt_long = (EditText) getActivity().findViewById(R.id.txt_longitude);
String long_str = txt_long.getText().toString();
}
}
------解决思路----------------------
放数据:getactivity().getIntent.putExtra(“date”,“heihei”);
取数据:getactivity().getIntent.getStringExtra("date");
当然取数据之前最好判空。
getactivity().getIntent.得到的intent 是同一个,我们完全可以用来传一些数据。
------解决思路----------------------
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
第一个fragment会被remove掉
建议用值传递的方式
------解决思路----------------------
挺多方法的:
1、Application
2、SharedPreferences
3、通过Activity作为桥梁进行传递