安卓。如何让按钮显示为 PRESSED,直到该按钮创建的操作完成?

标签 android button set pressed

我有 button_focusedbutton_pressedbutton_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/

相关文章:

swift - Swift 的 Set.indexOf 的时间复杂度

Java如何使用整数集对对象列表进行排序

python - 在python中获取彼此相交的集合列表的列表

java - Intent putExtra 持续存在

android - 探戈 : Why Intrinsic data from RGB camera and not from depth camera for point cloud projection?

java - 仅显示最后添加的按钮

Javascript 将按钮添加到表中

android - 如何获取Android Preferences DataStore的所有键

java - 如何使按钮转到另一个随机 Activity

JavaScript 输入焦点