0

我是这里的新手,刚刚在我的 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);
     }
     }
4

1 回答 1

0

经过长时间与这个问题的斗争,我已经能够找到在使用基于活动的选项卡时切换选项卡的解决方案。

在创建 tabhost 的父活动类中,我实现了如下方法:

public void switchTab(int tab){
        tabHost.setCurrentTab(tab);   

}

在我希望能够在内部切换到另一个选项卡的选项卡内部,我创建了以下方法:

public void switchTabInActivity(int indexTabToSwitchTo){
        MintTrack ParentActivity;
        ParentActivity = (MintTrack) this.getParent();
        ParentActivity.switchTab(indexTabToSwitchTo);

}

另请参阅此示例

于 2012-08-08T16:54:00.450 回答