Android Tab Button: handle tap/click event

2019-07-27 14:31发布

Please see the follow code fragment:

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HomeTabActivity.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home", 
               res.getDrawable(R.drawable.ic_tab_home)).setContent(intent);
tabHost.addTab(spec);

Now when I click on an tab button, it shows corresponding activity, but then I click the button again, I want to also detect this click, even if the tab is actually the "current" or active tab, is there anyway to do this? I did not find any set listener method in spec.

2条回答
家丑人穷心不美
2楼-- · 2019-07-27 14:45

Well, I don't think there is anything that does that as part of the tab control. There are a couple of things you could try though.

  1. Override the onNewIntent method on the HomeTabActivity and see if they send the intent every time that the tab is tapped, though I doubt it.
  2. You could try putting a listener on the view returned by the getCurrentTabView method on TabHost
查看更多
看我几分像从前
3楼-- · 2019-07-27 14:55

call below method using setListener(TabWidgetActivity); call it just before creating tabs.

    void setListener(final TabActivity tabActivity)
    {
        tabActivity.getTabHost().setOnTabChangedListener(new OnTabChangeListener()         {

            public void onTabChanged(String tabId) 
            {

            }
        });

        tabActivity.getTabHost().setOnLongClickListener( new OnLongClickListener() {    

            public boolean onLongClick(View v) 
            {

                return false;
            }
        });
    }
查看更多
登录 后发表回答