简体   繁体   English

防止TabHost标签更改?

[英]Prevent TabHost Tab Change?

Is there any way with the stock Android TabHost to prevent a tab change from taking place ??? 是否有任何方式与股票Android TabHost,以防止更改标签?

In iOS, there is a delegate callback called shouldSelectViewController on the tabBarController, which if you return FALSE prevents the tab change from taking place. 在iOS中,tabBarController上有一个名为shouldSelectViewController的委托回调,如果返回FALSE,则会阻止更改变量。

Android has an onTabChanged() delegate, but that appears to be an after-the-fact notification that the tab change has taken place (it returns void). Android有一个onTabChanged()委托,但这似乎是一个事后通知,表明变量发生了变化(它返回void)。

Thanks. 谢谢。

If you don't want to use onTabChanged() for that, you can set OnClickListener/OnTouchListener for each tab and do it there. 如果您不想为此使用onTabChanged(),可以为每个选项卡设置OnClickListener / OnTouchListener并在那里执行。 Example: 例:

    for(int i=0; i<tabWidget.getTabCount(); i++) {
        tabWidget.getChildAt(i).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();

                if (action == MotionEvent.ACTION_UP) {                      
                    String currentTabTag = (String) tabHost.getCurrentTabTag();
                    String clickedTabTag = (String) v.getTag();

                    if(clickedTabTag.equals("BAD TAG")) {
                        return true; // Prevents from clicking
                    }
                }
                return false;
            }               
        });
    }

If you want to prevent the tab from changing, for example you always want the tab be tab 0, try this code: 如果要阻止选项卡更改,例如,您始终希望选项卡为选项卡0,请尝试以下代码:

if (tabId.equals("Tab 1")) {
   // This will switch tab 1 to tab 0 immediately so that it seems like tab 0 does not change
  getTabHost().setCurrentTab(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM