我有 button_focused
、button_pressed
和 button_normal
图像。当我按下按钮时,将显示 button_pressed
图像,并且与按下按钮相关的操作开始。
当我停止按下按钮时,操作继续,但按钮返回到正在显示的 button_normal
图像。
如何在整个操作期间将正在显示的按钮图像设置为 button_pressed
,然后重置为 button_normal
图像?
谢谢你的时间
最佳答案
我使用了一个类似的函数
void setHighlighted(boolean highlight) {
button.setBackgroundResource( highlight
? R.drawable.bbg_pressed
: R.drawable.button_background);
}
其中 button_background 是定义在
button_backgroung.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/bbg_pressed"></item>
<item android:state_focused="true" android:drawable="@drawable/bbg_selected"></item>
<item android:drawable="@drawable/bbg_normal"></item>
</selector>
也就是说,这段代码不会干扰Android框架使用的按下状态;相反,它会更改背景,使按钮看起来已被按下。
关于安卓。如何让按钮显示为 PRESSED,直到该按钮创建的操作完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674940/