Android 中哪种按钮可以切换/更改状态?
所以我有一个图像按钮,我想在它上面有两种状态。它的工作方式类似于具有两种状态的书签按钮。如果用户按下按钮,那么它会更改图像,如果它重新按下按钮,那么我想要回原始图像。
这可能吗?有没有更简单的方法使用其他类型的按钮? 谢谢
So I have an image button and I want to have two states on it. It will work like a bookmark button which has two states. If the user presses the button then it changes the image and if it re-presses the button then I want the original image back.
Is that possible? Is there an easier way using another type of button?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将主题更改为:“哪种按钮可以切换/更改状态?”
似乎您需要
ToggleBotton
并且此 xml 定义了按钮在静止/按下状态下的颜色/图像,将其放入
res/color/button_colors.xml
Try changing the topic to :"What kind of Button can toggle/change states?"
Seems like you need
ToggleBotton
And this xml defines the colors/images of the button at rest/pressed states, put it in
res/color/button_colors.xml
是的,您也可以使用常规按钮。首先在 res/drawable 目录中创建一个选择器 XML 文件。示例:
在布局 xml 的按钮定义中,您只需添加以下属性:
然后您自己以编程方式设置选定状态(在 onClickListener 中),按钮图像将更改背景状态。
Yes, you can use regular buttons also. First make a selector XML file in the res/drawable directory. Example:
In the button definition in the layout xml, you just add this attribute:
And then you programmatically set the selected states yourself (in a onClickListener) and the button images will change background state.
我想您可以使用切换按钮并希望它能解决您的问题。
只需看一下它的简单 xml 代码:
并且只需检查此链接中给出的答案:
Android:使用 XML 为切换按钮指定两个不同的图像
还,
我发布了有关切换按钮的简单教程。如果您觉得有帮助,请看看我的简陋博客。这是链接: http://androiddesk.wordpress.com /2012/03/10/toggle-button-in-android/
I guess you can use Toggle Button and hope it'll solve your issue.
Just have a look at a simple xml code of it:
And Just check the answer given here in this link:
Android: Specify two different images for togglebutton using XML
Also,
I've posted a simple tutorial about Toggle Button. Just have a look at my humble blog if you find it helpful. Here is link: http://androiddesk.wordpress.com/2012/03/10/toggle-button-in-android/