什么样的按钮可以切换在Android中/更改状态吗? [英] What kind of Button can toggle/change states in Android?

查看:133
本文介绍了什么样的按钮可以切换在Android中/更改状态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个图像按钮,我想有这两种状态。它会像一个书签按钮,它有两种状态。如果用户presses的按钮,然后它改变了形象,如果再presses的按钮,然后我想原始图像回来。

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

推荐答案

试着改变话题:可以切换什么样的巴顿/更改状态

Try changing the topic to :"What kind of Button can toggle/change states?"

好像你需要 ToggleBotton

<ToggleButton                  
        android:layout_width="wrap_content"
        android:layout_height="26dp"
        android:background="@color/button_colors"
        android:button="@null"
        android:textOff="@null"
        android:textOn="@null" />

和这个XML定义了在休息/ pressed状态按钮的颜色/图像,把它放在 RES /彩色/ button_colors.xml

And this xml defines the colors/images of the button at rest/pressed states, put it in res/color/button_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/button_rest"/>
<item android:state_checked="true" android:drawable="@drawable/button_on" />    
</selector>

这篇关于什么样的按钮可以切换在Android中/更改状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆