我有4项活动。
第一个Activity用于输入数据,第二个Activity是TAB主要的Activity(扩展TabActivity)来声明每个标签Activity,第三个和第四个Activity是标签活动。
如何将值从第一个Activity转移到第三个和第四个Activity?
这是我的第一个活动:
public class FirstActivity extends Activity {
EditText inputName;
EditText inputAddress;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//declaration layout
inputName = (EditText) findViewById(R.id.editText1);
inputAddress = (EditText) findViewById(R.id.editText2);
Button btnNextScreen = (Button) findViewById(R.id.button1);
//Listening to button event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), MainTabActivity.class);
//Sending data to another Activity
//THIS IS THE VALUE I WANNA TRANSFER TO TAB ACTIVITY
nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("address", inputEmail.getText().toString());
// starting new activity
startActivity(nextScreen);
}
});
}
}
这是我的MainTabActivity:
public class MainTabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maintabresto);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, InfoTab.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("info").setIndicator("Info",
res.getDrawable(R.drawable.iconinfotab))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, MenuTab.class);
spec = tabHost.newTabSpec("menu").setIndicator("Menu",
res.getDrawable(R.drawable.iconmenutab))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
这是我的第3次第4次活动(我希望在每次活动中显示我之前转移到textview的值):
public class InfoTab extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infotab);
TextView txtname = (TextView) findViewById(R.id.textTEST1);
TextView txtaddress = (TextView) findViewById(R.id.TextTEST2);
//displaying data from previous activity
//this is didnt work
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String address = i.getStringExtra("address");
// Displaying Received data
//this is didnt work
txtname.setText(name);
txtaddress.setText(address);
}
}
谁能帮助我?
我试图在第一次活动中这样做:
Intent nextScreen = new Intent(getApplicationContext(), InfoTab.class);
它有效!(该值可以转移到nextactivity,但是在选项卡上打开活动,活动单独打开(不在标签上)......
所以我的目标是将价值转移到他们在MainTabActivity下面打开的每个标签活动。
抱歉,如果我的英语不好。答案 0 :(得分:0)
我认为您的代码中存在一些小错误。但我无法理解。
下面是我的tabActivity代码,其中我正在做同样的事情。
public class MyTabActivity extends TabActivity {
TabHost myTabHost;
String data1, data2, data3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_tab_activity);
Intent i = getIntent();
data1 = i.getStringExtra("Data1");
data2 = i.getStringExtra("Data2");
data3 = i.getStringExtra("Data3");
setTabs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_tab_activity, menu);
return true;
}
public void setTabs() {
myTabHost = getTabHost();
addTab(R.string.tab1Txt, R.drawable.video);
addTab(R.string.tab2Txt, R.drawable.pdf);
addTab(R.string.tab3Txt, R.drawable.notepad);
myTabHost.setCurrentTab(2);
}
public void addTab(int labelId, int drawableId) {
Intent tabIntent = null;
TabHost.TabSpec spec;
switch (labelId) {
case R.string.tab1Txt:
tabIntent = new Intent(this, Tab1Activity.class);
tabIntent.putExtra("Data", data1);
break;
case R.string.tab2Txt:
tabIntent = new Intent(this, Tab2Activity.class);
tabIntent.putExtra("Data", data2);
break;
case R.string.tab3Txt:
tabIntent = new Intent(this, Tab3Activity.class);
tabIntent.putExtra("Data", data3);
break;
}
spec = myTabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
title.setTextColor(Color.BLUE);
ImageView img = (ImageView) tabIndicator.findViewById(R.id.icon);
img.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(tabIntent);
myTabHost.addTab(spec);
}
}
希望它有所帮助。