Android 选项卡上的 OnClickListener 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1062476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:39:28  来源:igfitidea点击:

OnClickListener on Tabs not working

androidonclickandroid-tabhost

提问by Jorgesys

Greetings,

你好,

I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.

我试图在单击 TabActivity 的当前选定选项卡时获取 Click - 事件。onTabChangedHandler 仅在选项卡更改时调用,而不是在单击当前活动选项卡时调用。调试器告诉我我在 TabHost 中为 TabWidget 注册了 onClickListener。

Am i registering for the wrong View?

我是否注册了错误的视图?

Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?

另外,我无法为选项卡创建上下文菜单,只能为其内容创建,是否与此问题有关?

public class TestDroidViewTab extends TabActivity 
                              implements TabContentFactory
                              , OnTabChangeListener, OnClickListener {

  private static final String LOG_KEY = "TEST";
  ListView listView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final TabHost tabHost = getTabHost();


      TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
      ts.setIndicator("1"); 
      ts.setContent(this);
      tabHost.addTab(ts);

      ts = tabHost.newTabSpec("ID_2");
      ts.setIndicator("2"); 
      ts.setContent(this);
      tabHost.addTab(ts);

      ts = tabHost.newTabSpec("ID_3");
      ts.setIndicator("3"); 
      ts.setContent(this);
      tabHost.addTab(ts);
      tabHost.setOnClickListener(this);
      tabHost.setOnTabChangedListener(this);
  }
  public void onClick(View v) {
      Log.d(LOG_KEY, "OnClick");
  }

  public void onTabChanged(String tabId) {
      Log.d(LOG_KEY, "OnTabChanged");
  }

回答by Milo

If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.

如果您想看到某个特定选项卡被点击,您需要将您的侦听器添加到选项卡本身,而不是 TabHost。

The hierarchy of views in a tab implementation is:

选项卡实现中的视图层次结构是:

  • TabHost
    • TabWidget
      • (tab)
      • (tab)
    • FrameLayout
  • 标签主机
    • 标签小部件
      • (标签)
      • (标签)
    • 框架布局

The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));

通过调用在运行时添加选项卡: tabHost.addTab(tabHost.newTabSpec(""));

You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);

然后,您可以通过调用来获取各个选项卡的句柄: getTabWidget().getChildAt(4);

In essence, you are adding your OnClickListenerto a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListenerwill need to do that for you.

本质上,您是将您的添加OnClickListener到 TabWidget 的子项。您现在可以在您的个人标签上获取点击次数。但是,这将覆盖单击选项卡时更改内容的默认行为。因此,要改变您的内容,您OnClickListener需要为您做这件事。

Here is a full example, which lets you intercept the click event, and change the content below the tab:

这是一个完整的示例,它可以让您拦截单击事件,并更改选项卡下方的内容:

final String myTabTag = "My Tab";
final int myTabIndex = 3;

getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
            getTabHost().setCurrentTab(myTabIndex );
        }
    }
});

回答by Jorgesys

use setOnTabChangedListenerinstead of OnClickListener;)

使用setOnTabChangedListener而不是OnClickListener;)

    static TabHost tabHost;

    tabHost = getTabHost();


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
       @Override
      public void onTabChanged(String arg0) {
       Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
      }     
});  

回答by martin

Your clause is wrong, use:

您的条款是错误的,请使用:

...

...

if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
            getTabHost().setCurrentTab(myTabIndex );
   }

...

...

回答by dicenice

into my code, it shows some errors and ask me to create new methods in those names like getTabWidget(), getTabHost(), etc. Waiting for your response.

在我的代码中,它显示了一些错误,并要求我以这些名称创建新方法,例如 getTabWidget()、getTabHost() 等。等待您的回复。

Try this

尝试这个

 tabHost.getTabHost().setCurrentTab(myTabIndex);