如何检测已选中的选项卡按钮的单击事件

8

我正在尝试获取TabActivity当前选定选项卡上的Click事件。

我尝试了下面的代码,但当我点击一个选项卡时,其他选项卡不能正常工作/点击。

    setupTab(new TextView(this), "Map");
    setupTab(new TextView(this), "Attacks");
    setupTab(new TextView(this), "Profile");
    setupTab(new TextView(this), "Headquater");

    int numberOfTabs = tabHost.getTabWidget().getChildCount();
    for(int t=0; t<numberOfTabs; t++){
    getTabWidget().getChildAt(t).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(tabHost.getCurrentTab() == 0){
                    Toast.makeText(TabContext, ""+"i m in on clickkkkk" ,1500).show();
                    getTabHost().setCurrentTab(0);
                }
                if(tabHost.getCurrentTab() == 1){
                    Toast.makeText(TabContext, ""+"i m in on clickkkkk....$#@$#$" ,1500).show();
                    getTabHost().setCurrentTab(1);
                }

            }
        });
    }
3个回答

9

我需要仅针对一个选项卡检测第二次单击,所以这个答案对我很有用。诀窍在于为子元素设置OnClick,而不是为TabHost本身设置。

getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("TAG", "Clicked tab : "+getTabHost().getCurrentTabTag());
        tabHost.setCurrentTab(0);                                    
    }
});

这个工作非常完美,即使对于复杂的tabhost/fragments设置也是如此。谢谢! - MacD

3
我使用了theczechsensation的解决方案,但进行了一些小修改,因为将监听器添加到子级而不是选项卡主机本身会导致混淆,为了更好地分类,请阅读代码中的注释。
 mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // re enforce selecting targeted tab to re apply main listener to default behaviour
                mTabHost.setCurrentTab(0);
                // display current tab tag in console
                Log.i("MainActivity", "Clicked tab : "+mTabHost.getCurrentTabTag());
                // identify targeted fragment
                Fragment currentFragment = new "FragmentClassName"();
                // execute navigation (fragment transaction)
                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, currentFragment)
                        .commit();
                // re enforce selecting targeted tab to re apply main listener to default behaviour
                mTabHost.setCurrentTab(0);
            }
        });

2

在你的onCreate方法内:

for(int i = 0; i < TOTAL_TAB; i++) {  
    tabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
    tabHost.getTabWidget().getChildAt(i).setTag(i+"");  
}  

your listener:
@Override

public boolean onTouch(View v, MotionEvent event) {
    if(MotionEvent.ACTION_DOWN == event.getAction()) {
        previousTab = tabHost.getCurrentTab();
        currentTab = Integer.parseInt((String)v.getTag());
        if(previousTab == currentTab) {
            if(currentTab == 0){
                Log.i("log", "0th same tab selected");
            }else if(currentTab == 1){
                Log.i("log", "1st same tab selected");
            }else if(currentTab == 2){
                Log.i("log", "2nd same tab selected");
            }else if(currentTab == 3){
                Log.i("log", "3rd same tab selected");
            }
            return true;
        }
    }
    return false;
}

2
并且,TabHost tabHost = getTabHost(); - Bhavana Vadodariya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接