0

我有一个带有 3 个标签的标签主机。在选项卡 3 中,我有一个活动:testActivity。在这个活动中,我有一个按钮。当我在选项卡 3 中单击此按钮时,我希望选项卡主机在选项卡 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");    
    }
}
4

2 回答 2

0

首先尝试在您的onClick方法中使用对您的 tabHost 的引用getParent()

于 2012-06-05T04:20:59.120 回答
0
public void btn_onClick(View v)
{
    LocalActivityManager l = new LocalActivityManager ();
    l.tabHost.setCurrentTab(0);
}
于 2012-06-05T04:27:42.620 回答