简体   繁体   中英

How come my button does not move to another screen?

I am a rookie here and have just created a multiple tab screens on my android app but when i try to create a button on one of the multiple tab screen, the button when pressed did not move to another screen that i create. I was wondering if I need to add any code in my multiple tab screen to make the button works. Here's my java code for my multiple tab:

    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); 

}

Code for my button

    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);
       }
    });
    }
    }

code for the screen when i press the button

    public class EquitySelection extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.equitydescription);
     }
     }

After a long time of battling with this problem I've been able to find a solution to switching tabs when using activity based tabs.

In the parent activity class where the tabhost is created I implemented a method like the one below:

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

}

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

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

}

Also see this Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM