Android - 如何在tabhost中自动切换选项卡

时间:2012-06-05 04:17:20

标签: android android-tabhost

我有一个包含3个标签的tabhost。在标签3中,我有一项活动:testActivity。在这个活动中,我有一个按钮。当我在标签3中单击此按钮时,我希望tabhost在标签1中切换。它应该自动切换;我没有点击标签1.我该怎么做?

public class TabhostActivity extends Activity{
public TabHost tabHost;
private Resources res;
Intent intent;
TabHost.TabSpec spec;
private LocalActivityManager mLocalActivityManager;
protected int currentTab;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabhost);
    tabHost = (TabHost) findViewById(R.id.tabhost);
    mLocalActivityManager = new LocalActivityManager(this, false);
    mLocalActivityManager.dispatchCreate(savedInstanceState);
    tabHost.setup(mLocalActivityManager);
    res = getResources();
    intent = new Intent().setClass(this, SpinnerActivity.class);
    spec = tabHost.newTabSpec("artists");

    spec.setIndicator("Sinner", null);
    spec.setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SonAndroidActivity.class);
    spec = tabHost.newTabSpec("MAIN");
    spec.setIndicator("MAIN",res.getDrawable(R.drawable.ios_logo));
    spec.setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent().setClass(this, testActiviy.class);
    spec = tabHost.newTabSpec("Spinner");
    spec.setIndicator("TEST", null);
    spec.setContent(intent);
    tabHost.addTab(spec);
}

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            Log.v("tag", "click:" + tabHost.getCurrentTab());
            currentTab = tabHost.getCurrentTab();                   
}

和活动活动标签3:

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

    public void btn_onClick(View v)
    {   
        tabHost.setCurrentTab(0);
        Log.v("tag", "clickkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");    
    }
}

2 个答案:

答案 0 :(得分:0)

首先尝试使用onClick

getParent()方法中引用您的tabHost

答案 1 :(得分:0)

public void btn_onClick(View v)
{
    LocalActivityManager l = new LocalActivityManager ();
    l.tabHost.setCurrentTab(0);
}