Android Tab Button: handle tap/click event

好久不见. 提交于 2020-01-06 00:02:29

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/5985907/android-tab-button-handle-tap-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!