简体   繁体   English

检测对已选择的选项卡按钮的单击

[英]Detecting a click on an already selected tab button

Is there a way to detect the user tapping on a tab button in a TabWidget? 有没有一种方法可以检测到用户点击了TabWidget中的选项卡按钮?

I know about OnTabChangeListener, but I want to detect the tap on a tab button even if that tab was already selected. 我知道OnTabChangeListener,但我想检测选项卡按钮上的点击,即使该选项卡已被选中。

This is needed to copy a behavior that is standard with iOS. 复制iOS的标准行为时需要这样做。 Take for instance the App Store App: Choose the "Categories" tab. 以App Store应用为例:选择“类别”标签。 Navigate a few levels deep into the categories. 浏览几个级别深入到类别。 Then tap again on the "Categories" tab. 然后再次点击“类别”选项卡。 This will bring you back to the top level. 这将带您回到顶层。 (as an alternative to tapping multiple times on the back button). (作为多次点击“后退”按钮的替代方法)。 I find this very intuitive and therefore also applicable to Android. 我觉得这非常直观,因此也适用于Android。

This question was asked before: Android Tab Button: handle tap/click event . 之前曾有人问过这个问题: Android Tab按钮:处理点击事件 However the accepted answer there is not useful. 但是,那里接受的答案没有用。

I tried OnClickListener and OnTouchListeners on the TabWidget. 我在TabWidget上尝试了OnClickListener和OnTouchListeners。 But they are never triggered. 但是它们永远不会被触发。

Hide the tab widget and use standard buttons to switch between tabs. 隐藏选项卡小部件并使用标准按钮在选项卡之间切换。 You can switch tabs using 您可以使用以下方式切换标签

tabhost.setCurrentTab('index of the tab');

You can make the button look selected by using the State drawable and setting the button as selected. 您可以通过使用State drawable并将按钮设置为选定状态来使按钮看起来处于选中状态。

button.setSelected(true);

Well, to do your requirement, you can put the appropriate code in the buttons onClick listener. 好了,为了满足您的要求,您可以将适当的代码放入onClick侦听器的按钮中。 This project might give you an idea on setting up the tabs. 项目可能会给您一个设置标签的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM