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

            }
        });
    }
4

3 回答 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);                                    
    }
});
于 2012-11-05T06:40:45.283 回答
3

我使用了 theczechsensation 解决方案,但进行了少量修改,因为将侦听器添加到子项而不是选项卡主机本身会导致 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);
            }
        });
于 2014-12-25T13:03:54.730 回答
2

在您的onCreate方法中:

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

你的听众
@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;
}
于 2013-07-26T08:07:56.067 回答