安卓。如何使按钮保持显示为“已按下”,直到该按钮创建的操作完成为止?
我有 button_focused
、button_pressed
和 button_normal
图像。当我按下按钮时,将显示 button_pressed
图像,并且与按下按钮相关的操作开始。
当我停止按下按钮时,操作会继续,但按钮会返回到显示的 button_normal
图像。
如何将整个操作期间显示的按钮图像设置为 button_pressed
,然后重置为 button_normal
图像?
谢谢您的宝贵时间
I have button_focused
, button_pressed
, and button_normal
images. When I press the button, the button_pressed
image is displayed and the action related to the button pressing begins.
When I quit pressing the button, the action continues but the button returns to button_normal
image being displayed.
How can I set the button image being displayed to button_pressed
during the entire action then reset to the button_normal
image?
Thank you for your time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我使用了一个类似的函数
,其中button_background是定义在中的选择器
button_backgroung.xml
:即这段代码不会干扰Android框架使用的按下状态;相反,它会更改背景,使按钮看起来被按下。
I used a function like
where button_background is a selector defined in
button_backgroung.xml
:That is, this code does not interfere with the pressed state used by the Android framework; instead, it changes the background so that the button looks pressed.
如果您只需要两个状态按钮,则更清楚一点:
您不需要自己的
button.xml
。您可以正常使用Android。如果您单击按钮,
button.setPressed(true)
将不起作用,因为一旦您松开按钮,Android 就会重置它。首先尝试设置另一个按钮的setPressed
状态来查看效果。这意味着,要在同一个按钮上使用它,必须将其设置为延迟。这是一个工作示例。当然,上面提到的改变背景的方法(通过长 id 18..)也有效。
To make it a bit clearer if you are just in need of a two state button:
You do not need your own
button.xml
. You can work with the normal of Android.The
button.setPressed(true)
will not work if you are clicking the button, because Android will reset it once you let go of the button. Try to set another buttonssetPressed
state first to see the effect.Which means, to use it on the same button it must be set delayed. Here is a working example. Of course, the mentioned approach (by long id 18..) on changing the background works too.
如果您在 onClick 方法中手动更改按钮中的图像,那么当操作完成时,它可以设置该按钮的正常图像。如果操作非常快,则更改将无法正确显示 - 它可能还需要延迟代码。
If you change the image in the button manually in its onClick method, then when an action finishes it can set the normal image for that button back. If the action is very quick then change will not show properly - it may need a delay code as well.
使用
(buttonName).setPressed(true)
并确保您已为可绘制对象保留了适当的 xml 文件,该文件定义了用于按下、聚焦等状态的可绘制对象:
Use
(buttonName).setPressed(true)
And make sure you have kept the appropriate xml file for the drawable that defines which drawable to use for states like pressed,focused etc:
然后
,当操作终止时,我复制了代码并插入了
button_normal
的整数值。我之前这样做的方式不同,但我找不到我的代码的备份或硬拷贝。
再次感谢您的回复。
I used
then when the action terminates I copied the code and inserted the integer value of
button_normal
.I did this differently before and I cannot find my backups or hard copies of my code.
Thank you again for your responses.
只是加上我的 2 美分作为另一种选择。您可以使用setSelected(true),而不是使用button.setPressed,一旦用户松开按钮,选择器就会丢失选择器的状态。只要确保完成操作后返回 setSelected(false) 即可。
并将可绘制选择器添加为按钮的背景:
Just adding my 2 cents as another alternative. Instead of button.setPressed, which will lose the state from the selector once the user let go of the button, you can use setSelected(true). Just make sure to go back with setSelected(false) when you finish the action.
And add the drawable selector as background for the button:
老问题,但这是我所做的,看起来比上述所有问题都简单。在按钮的 onclick 中:
我的按钮正在使用可绘制资源(不确定这是否重要),当用户单击一个资源时,我设置了我想要的颜色过滤器,该颜色过滤器与我用于“按下”状态的颜色相同,因此看起来像它保持按下状态。
要清除它,请使用:
Old question but here's what I did that seems simpler than all of the above. In the button's onclick:
My buttons are using a drawable resource (not sure if that matters) and when the user clicks one I set the color filter I want which is the same color I use for the "pressed" state so it looks like it stays pressed.
To clear it use: