简体   繁体   English

如何检测已选择的选项卡按钮上的单击

[英]how to detecting a click on an already selected tab button

I am trying to get the Click event when clicking on currently selected tab of my TabActivity. 我在点击TabActivity当前选中的标签时尝试获取Click事件。

I tried below code but when i click on one tab the other tabs are not working/clicking properly. 我尝试下面的代码,但当我点击一个选项卡时,其他选项卡无法正常工作/单击。

    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);
                }

            }
        });
    }

I needed to detect second click only for one tab, so this answer worked for me. 我需要仅针对一个选项卡检测第二次单击,因此这个答案对我有用。 The trick is setting the OnClick for the child and not for the TabHost itself. 诀窍是为孩子设置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);                                    
    }
});

I used theczechsensation solution but with minor modifications because adding a listener to child and not to the tab host itself will cause confusion to tabhost listener to read comments in the code for better classifications 我使用了捷克语解决方案但稍作修改,因为向子级而不是向标签主机本身添加监听器会导致tabhost侦听器混淆以读取代码中的注释以获得更好的分类

 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);
            }
        });

Inside your onCreate method: 在你的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;
}

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

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