ActionBar按钮可以保持“按下”状态吗?

时间:2021-06-04 19:44:46

I have an app that in a certain Activity (drawing) can be in any one of a number of states, such as draw, erase, select, etc.

我有一个应用程序,在某个活动(绘图)中可以处于多种状态中的任何一种状态,例如绘制,擦除,选择等。

To enable these states, I've got a button for each in the ActionBar, but one thing I'd like to do is to 'show' the user which state is enabled by keeping the button pressed active or pressed until I turn it off (when they have switched state by pressing one of the other buttons).

为了启用这些状态,我在ActionBar中为每个状态设置了一个按钮,但我想做的一件事是通过保持按下按钮激活或按下直到我将其关闭来“显示”用户启用哪个状态(当他们通过按其中一个按钮切换状态时)。

Searches here and other places have me coming up blank... can anyone recommend a possible solution? I've though about rolling my own toolbar, and while this might be my final solution, using the ActionBar would speed things up greatly at this point.

这里和其他地方的搜索让我空白......任何人都可以推荐一个可能的解决方案吗?我虽然想要滚动我自己的工具栏,虽然这可能是我的最终解决方案,但使用ActionBar会在这一点上大大加快速度。

Thanks.

谢谢。

3 个解决方案

#1


0  

There is no direct way to do that, as far as I know, but you could implement your custom component to do that. However, my advice to you is having a separate toolbar that's not on the action bar, since the users expect all buttons on the Action Bar to be "Action Items" which perform something immediate, so having toggle items on the Action Bar might break this expectation. This allows you to save space on the action bar for things that the user expects to see there like: navigation, "Undo", "Save", "Delete"...

据我所知,没有直接的方法可以做到这一点,但你可以实现自定义组件来做到这一点。但是,我给你的建议是有一个单独的工具栏,它不在操作栏上,因为用户希望操作栏上的所有按钮都是“Action Items”,它可以立即执行某些操作,因此在Action Bar上切换项可能会破坏这个期望。这允许您在操作栏上节省用户希望在那里看到的内容的空间:导航,“撤消”,“保存”,“删除”......

#2


0  

I agree with Bruno about the fact its probably best if you do it in a separate toolbar for the sake of user experience standard and your code will probably look better because you cant fully customize the action bar but if you do decide to go with it i can think of something really simple like, when clicking one of the menu buttons you set the pressed button to a new drawable (pressed button) and the others to their normal drawable (not pressed) so each menu icon will have a pressed and not pressed icon. you will have to invalidateOptionsMenu though.

我同意布鲁诺关于这样一个事实,如果你为了用户体验标准在单独的工具栏中做它可能是最好的,你的代码可能会看起来更好,因为你无法完全自定义操作栏但是如果你决定使用它我可以想到一些非常简单的东西,当点击其中一个菜单按钮时,你将按下的按钮设置为一个新的drawable(按下按钮),其他按钮设置为正常的drawable(未按下),这样每个菜单图标都会被按下而不是按下图标。你必须使invalidateOptionsMenu无效。

#3


0  

I had the same problem and did not find a solution. However, I found a workaround that works for me:

我有同样的问题,没有找到解决方案。但是,我找到了一个对我有用的解决方法:

In the onOptionsItemSelected(MenuItem item) method, if item is the button you want to toggle, simply call item.setIcon() and point to another drawable. I use the same image but with another color to show the user that the button is in "pressed" state. And when the button is pressed again, revert to the original drawable.

在onOptionsItemSelected(MenuItem item)方法中,如果item是要切换的按钮,只需调用item.setIcon()并指向另一个drawable。我使用相同的图像但使用另一种颜色向用户显示按钮处于“按下”状态。再次按下该按钮时,恢复原来的drawable。

#1


0  

There is no direct way to do that, as far as I know, but you could implement your custom component to do that. However, my advice to you is having a separate toolbar that's not on the action bar, since the users expect all buttons on the Action Bar to be "Action Items" which perform something immediate, so having toggle items on the Action Bar might break this expectation. This allows you to save space on the action bar for things that the user expects to see there like: navigation, "Undo", "Save", "Delete"...

据我所知,没有直接的方法可以做到这一点,但你可以实现自定义组件来做到这一点。但是,我给你的建议是有一个单独的工具栏,它不在操作栏上,因为用户希望操作栏上的所有按钮都是“Action Items”,它可以立即执行某些操作,因此在Action Bar上切换项可能会破坏这个期望。这允许您在操作栏上节省用户希望在那里看到的内容的空间:导航,“撤消”,“保存”,“删除”......

#2


0  

I agree with Bruno about the fact its probably best if you do it in a separate toolbar for the sake of user experience standard and your code will probably look better because you cant fully customize the action bar but if you do decide to go with it i can think of something really simple like, when clicking one of the menu buttons you set the pressed button to a new drawable (pressed button) and the others to their normal drawable (not pressed) so each menu icon will have a pressed and not pressed icon. you will have to invalidateOptionsMenu though.

我同意布鲁诺关于这样一个事实,如果你为了用户体验标准在单独的工具栏中做它可能是最好的,你的代码可能会看起来更好,因为你无法完全自定义操作栏但是如果你决定使用它我可以想到一些非常简单的东西,当点击其中一个菜单按钮时,你将按下的按钮设置为一个新的drawable(按下按钮),其他按钮设置为正常的drawable(未按下),这样每个菜单图标都会被按下而不是按下图标。你必须使invalidateOptionsMenu无效。

#3


0  

I had the same problem and did not find a solution. However, I found a workaround that works for me:

我有同样的问题,没有找到解决方案。但是,我找到了一个对我有用的解决方法:

In the onOptionsItemSelected(MenuItem item) method, if item is the button you want to toggle, simply call item.setIcon() and point to another drawable. I use the same image but with another color to show the user that the button is in "pressed" state. And when the button is pressed again, revert to the original drawable.

在onOptionsItemSelected(MenuItem item)方法中,如果item是要切换的按钮,只需调用item.setIcon()并指向另一个drawable。我使用相同的图像但使用另一种颜色向用户显示按钮处于“按下”状态。再次按下该按钮时,恢复原来的drawable。