问题描述
我有 button_focused
、button_pressed
和 button_normal
图像.当我按下按钮时,会显示 button_pressed
图像并开始与按钮按下相关的操作.
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.
当我停止按下按钮时,动作继续,但按钮返回到正在显示的 button_normal
图像.
When I quit pressing the button, the action continues but the button returns to button_normal
image being displayed.
如何在整个操作过程中将显示的按钮图像设置为 button_pressed
然后重置为 button_normal
图像?
How can I set the button image being displayed to button_pressed
during the entire action then reset to the button_normal
image?
感谢您的时间
推荐答案
我使用了一个类似
void setHighlighted(boolean highlight) {
button.setBackgroundResource( highlight
? R.drawable.bbg_pressed
: R.drawable.button_background);
}
其中 button_background 是定义在button_backgroung.xml
:
where button_background is a selector defined inbutton_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框架使用的按下状态;相反,它改变了背景,使按钮看起来被按下.
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.
这篇关于安卓.在该按钮创建的操作完成之前,如何保持按钮显示为 PRESSED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!