使用tabhost验证后的选项卡导航

时间:2015-07-29 13:51:17

标签: java android android-tabhost

我是使用TabHost的新手,目前正尝试在标签之间移动时实施验证检查。我的问题是使用

onTabChanged(String tabId)

因为我需要知道所选标签是什么,如果验证失败,我可以重定向到以前的标签。如何获取ID以便了解当前标签并重定向到上一个标签?

我的tabhost脚本是:

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;


public class Invoice3 extends FragmentActivity implements OnTabChangeListener {
private FragmentTabHost mTabHost;
String check = null;
ShowAlert alert = new ShowAlert();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main2);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Client", null),
            clientFragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Products", null),
            productsFragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Confirm", null),
            confirmFragmentTab.class, null);

}

public void onTabChanged(String tabId) {
    //Log.d(TAG, "onTabChanged(): tabId=" + tabId);
    check = GlobalApp.data().value;

    if (<check if 2nd tab has been selected>) {

        if(check == "False")
        {
          <redirect to 1st tab>
        }
    }

    }
}


}

1 个答案:

答案 0 :(得分:0)

您可以在变量中保存当前标签的ID,然后返回到它。这样的事情应该有效。

String prevTab = null;
public void onTabChanged(String tabId) {
    //Log.d(TAG, "onTabChanged(): tabId=" + tabId);
    check = GlobalApp.data().value;
    if (<check if 2nd tab has been selected>) {
        if(check == "False")
        {
            if (prevTab != null) {
                // redirect to prevTab
            }
        } else {
            prevTab = tabId;
        }
    }
}