在我的Android应用程序中,我想将值传递给选项卡...我有一个mainActivity然后维护活动和三个选项卡活动,如tab1 tab2和tab3 ..我需要将三个不同的值传递给tab1,tab2,tab3 ..我能够传递值...但是当我传递值时,结果不显示在内部选项卡中,而是作为单独的页面显示..我在下面给出我的代码..请帮助..
MainActivity
//Intent for tab operations
//Personal Year tab operation
Intent itab = new Intent(this, TabPersonalYr.class);
//Personal Year Number
itab.putExtra("name18",sum18 + "");
startActivity(itab);
//Personal Month tab operation
Intent imonthtab = new Intent(this, TabPersonalMnth.class);
//Personal Month Number
imonthtab.putExtra("name19",sum19 + "");
startActivity(imonthtab);
//Personal Month tab operation
Intent idaytab = new Intent(this, TabPersonalDay.class);
//Personal Day Number
idaytab.putExtra("name20",sum20 + "");
startActivity(idaytab);*/
MainTabLayout活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablay_xm);
TabHost tabHost = getTabHost();
// Tab for PersonalYear
TabSpec photospec = tabHost.newTabSpec("Personal Year");
// setting Title and Icon for the Tab
photospec.setIndicator("Personal Year", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, TabPersonalYr.class);
photospec.setContent(photosIntent);
// Tab for Personal Month
TabSpec songspec = tabHost.newTabSpec("Personal Month");
songspec.setIndicator("Personal Month", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, TabPersonalMnth.class);
songspec.setContent(songsIntent);
// Tab for PersonalDay
TabSpec videospec = tabHost.newTabSpec("Personal Day");
videospec.setIndicator("Personal Day", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, TabPersonalDay.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
tab1Activity
TextView txt2 = (TextView) findViewById (R.id.textView14);
txt2.setText(getIntent().getStringExtra("name18"));
答案 0 :(得分:1)
您可以使用create singleton Class传递值
public class ShareData{
private volatile static ShareData shareData;
public static ShareData data(){
if(shareData == null){
synchronized (ShareData.class) {
if (shareData == null) {
shareData = new ShareData();
}
}
}
return shareData;
}
public String value;
}
在Tab2中设置值
ShareData.data().value = "set in tab2";
在Tab1中获取值
txt2.setText(ShareData.data().value);