我是这里的新手,刚刚在我的Android应用程序上创建了一个多选项卡屏幕但是当我尝试在多个选项卡屏幕上创建一个按钮时,按下时按钮没有移动到我创建的另一个屏幕。我想知道是否需要在我的多选项卡屏幕中添加任何代码以使按钮工作。这是我的多选项卡的java代码:
public class Investment extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_investment);
TabHost tabHost = getTabHost();
// Tab for Homepage
TabSpec photospec = tabHost.newTabSpec("Home");
// setting Title and Icon for the Tab
photospec.setIndicator("Home", getResources().getDrawable(R.drawable.homeicon));
Intent photosIntent = new Intent(this, Home.class);
photospec.setContent(photosIntent);
// Tab for Riskassessment
TabSpec songspec = tabHost.newTabSpec("Risk");
songspec.setIndicator("Risk", getResources().getDrawable(R.drawable.riskicon));
Intent songsIntent = new Intent(this, RiskAssessment.class);
songspec.setContent(songsIntent);
// Tab for News
TabSpec videospec = tabHost.newTabSpec("News");
videospec.setIndicator("News", getResources().getDrawable(R.drawable.newsicon));
Intent videosIntent = new Intent(this, News.class);
videospec.setContent(videosIntent);
// Tab for Tips
TabSpec tipsspec = tabHost.newTabSpec("Tips");
tipsspec.setIndicator("Tips", getResources().getDrawable(R.drawable.investmenttipsicon));
Intent tipsIntent = new Intent(this, InvestmentTips.class);
tipsspec.setContent(tipsIntent);
// Tab for about us
TabSpec aboutusspec = tabHost.newTabSpec("About Us");
aboutusspec.setIndicator("About Us", getResources().getDrawable(R.drawable.aboutusicon));
Intent aboutusIntent = new Intent(this, AboutUs.class);
aboutusspec.setContent(aboutusIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding tab
tabHost.addTab(songspec);
tabHost.addTab(videospec);
tabHost.addTab(tipsspec);
tabHost.addTab(aboutusspec);
}
我的按钮代码
public class ButtonSelection extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button b = (Button) findViewById(R.id.buttonequity);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ButtonSelection.this, EquitySelection.class);
startActivity(i);
}
});
}
}
按下按钮时的屏幕代码
public class EquitySelection extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.equitydescription);
}
}
答案 0 :(得分:0)
经过长时间的努力解决这个问题后,我已经找到了在使用基于活动的标签时切换标签的解决方案。
在创建tabhost的父活动类中,我实现了如下所示的方法:
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
在我希望能够在内部切换到另一个标签的标签内,我创建了以下方法:
public void switchTabInActivity(int indexTabToSwitchTo){
MintTrack ParentActivity;
ParentActivity = (MintTrack) this.getParent();
ParentActivity.switchTab(indexTabToSwitchTo);
}
另见Example