TabHost的中第一个tab中button点击跳转到第二个tab的解决办法

时间:2022-11-09 16:39:09

public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     TabHost  tabHost  =    getTabHost();
;
      
        TabSpec  spec  =  tabHost.newTabSpec("tab1").setIndicator("first").setContent(new  Intent(this, First.class));
        TabSpec  spec2  =   tabHost.newTabSpec("tab2").setIndicator("second").setContent(new Intent(this, Second.class));
        //TabSpec  spec  =  tabHost.newTabSpec("tab1");spec..... 这样写是不对
/*        spec.setIndicator("first").setContent(new  Intent(this, First.class)); 
        spec.setIndicator("second").setContent(new Intent(this, Second.class));*/
        tabHost.addTab(spec);
        tabHost.addTab(spec2);
        tabHost.setCurrentTab(0);
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

   
}

 

 

重点在第一个tab里面有个按钮点击跳转到第二个tab中 

public class First  extends  Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.first);
  Button  button =   (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
   TabHost  tabHost  = (TabHost) getParent().findViewById(android.R.id.tabhost);
   tabHost.setCurrentTab(1);
   }
  });
 }

 

 

 

第二个tab

public class Second   extends  Activity{
  @Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.second);
}
}