2012-02-21 75 views
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); 
       } 

      } 
     }); 
    } 

回答

9

我需要檢測第二次點擊只有一個標籤,所以this answer爲我工作。訣竅是爲孩子設置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);          
    } 
}); 
+0

這工作完全,即使是複雜的tabhost /片段設置。謝謝! – MacD 2013-06-21 07:46:09

1

裏面你的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; 
} 
+1

和是的TabHost tabHost = getTabHost(); – 2013-07-26 08:08:44

3

我用theczechse nsation解決方案,但稍作修改,因爲添加監聽到孩子,而不是標籤主機本身會造成混亂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); 
      } 
     }); 
相關問題