Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been struggling with this for a few days now. I'm trying to switch tabs programmatically upon a button click. My program works flawlessly if I just use the tabs to change activities, but wiring an onClick method with setCurrentTab results in an error. This is the method that will not work. It's a pretty basic and straightforward function but I haven't seen much documentation or examples of people attempting to wire a buttonclick with switching tabs. Thanks.

ImageButton next = (ImageButton) findViewById(R.id.ButtonAsk);
 next.setOnClickListener(new View.OnClickListener() 
         {         
  public void onClick(View view)  
             {

                TabHost tabHost =  (TabHost) findViewById(android.R.id.tabhost);
              tabHost.setCurrentTab(2);                
             }
         });

See the edit history for the error log.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's a code example that you can put inside your onClick(). It's as Mark and Kevin described.

    TabActivity tabs = (TabActivity) getParent();
    tabs.getTabHost().setCurrentTab(2);

I've used this code tidbit numerous times. Hope this clarifies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...