在Android活动中切换TabActivity

时间:2011-12-14 16:12:20

标签: android tabactivity tabwidget

我正在尝试更改当前的Tab。我寻找合适的问题,但它没有解决我自己的问题。 我有像这样的Tab活动

<!-- language: java -->   
public class myTabActivity extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final TabHost mtabHost = getTabHost();
    int tabnum = getIntent().getIntExtra("currenttab", 0);


    mtabHost.addTab(mtabHost.newTabSpec("tab1")
            .setIndicator("firsttab")
                .setContent(new Intent(this, tabA.class)));
    mtabHost.addTab(mtabHost.newTabSpec("tab1")
            .setIndicator("secondtab")
                .setContent(new Intent(this, tabB.class)));
     setCurrentTab(tabnum);
}
private boolean setCurrentTab(int i) {
    if (getParent() instanceof SimpleSundayScoutUIActivity) {
        ((SimpleSundayScoutUIActivity) getParent()).getTabHost().setCurrentTab(i);
        return true;
    }
    return false;
}

和这样的活动标签

public class text extends Activity {
myTabActivity mA = (myTabActivity) this.getParent();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mainpage);
    Button startbutton= (Button) findViewById(R.id.searchbutton);
    startbutton.setOnClickListener(mNewListener);
}
private OnClickListener mNewListener = new OnClickListener() {

    public void onClick(View v) {
       Intent intent = new Intent(v.getContext(),SimpleSundayScoutUIActivity.class);
       intent.putExtra("currenttab", 1);
       startActivity(intent);
    }
};

意味着我想要做的是点击tabA的按钮然后选项卡小部件应该显示tabB但它不会像这样工作

有什么建议吗? sry我不太适合引用希望它是可读的

感谢您的帮助 帕特里克

修改

我想做的事情http://tinypic.com/r/f6uyq/5

1 个答案:

答案 0 :(得分:0)

编辑:

标签类:

public class myTabActivity extends TabActivity {
public static TabHost mtabHost; // change to public static
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     mtabHost = getTabHost();
    int tabnum = getIntent().getIntExtra("currenttab", 0);


    mtabHost.addTab(mtabHost.newTabSpec("tab1")
            .setIndicator("firsttab")
                .setContent(new Intent(this, tabA.class)));
    mtabHost.addTab(mtabHost.newTabSpec("tab1")
            .setIndicator("secondtab")
                .setContent(new Intent(this, tabB.class)));
     setCurrentTab(tabnum);
}
private boolean setCurrentTab(int i) {
    if (getParent() instanceof SimpleSundayScoutUIActivity) {
        ((SimpleSundayScoutUIActivity) getParent()).getTabHost().setCurrentTab(i);
        return true;
    }
    return false;
}

按钮类:

public class text extends Activity {
myTabActivity mA = (myTabActivity) this.getParent();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mainpage);
    Button startbutton= (Button) findViewById(R.id.searchbutton);
    startbutton.setOnClickListener(mNewListener);
}
private OnClickListener mNewListener = new OnClickListener() {

    public void onClick(View v) {
     myTabActivity.mtabHost.setCurrentTab(1); //Remove .this 

    }
};