java - setEnabled(false) -> (true)/selectableItemBackground 作为可绘制后没有可见的按钮反馈?

标签 java android button

通过一个按钮,我可以使用 button.setEnabled(false) 禁用其他三个按钮。
效果很好。
当再次按下此按钮时,树按钮将通过button.setEnabled(true)启用。
他们仍在做他们应该做的事情,并且很好地响应 onClickListener。
但由于启用,当它们被按下时,它们不会做出明显的响应。

我该如何重新激活它们?
(我已经在谷歌搜索但没有找到任何东西)。

<小时/>
private void startSleeping()
{
    editorState.putBoolean("SLEEPING", true);
    editorState.commit();

    buttonDrink.setEnabled(false);
    buttonEat.setEnabled(false);
    buttonWash.setEnabled(false);
    buttonDrink.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonEat.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonWash.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonSleep.setBackgroundColor(getResources().getColor(R.color.orange));
    buttonWash.setTextColor(getResources().getColor(R.color.lightgray));
    buttonDrink.setTextColor(getResources().getColor(R.color.lightgray));
    buttonEat.setTextColor(getResources().getColor(R.color.lightgray));
    buttonSleep.setTextColor(getResources().getColor(color.black));
}

private void stopSleeping()
{
    editorState.putBoolean("SLEEPING", false);
    editorState.commit();

    buttonDrink.setEnabled(true);
    buttonEat.setEnabled(true);
    buttonWash.setEnabled(true);
    buttonDrink.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonEat.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonWash.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonSleep.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonWash.setTextColor(getResources().getColor(R.color.white));
    buttonDrink.setTextColor(getResources().getColor(R.color.white));
    buttonEat.setTextColor(getResources().getColor(R.color.white));
    buttonSleep.setTextColor(getResources().getColor(R.color.white));
}
<小时/>
        <Button
            android:id="@+id/buttonEat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:paddingBottom="@dimen/padding_size"
            android:paddingTop="@dimen/padding_size"
            android:text="@string/button_eat"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="@dimen/text_size" />

enter image description here

最佳答案

这是因为您将背景资源设置为透明,而不是在布局中应用的背景资源作为这些按钮的可绘制背景。重新应用这些背景可绘制对象,您将再次获得触摸反馈。这应该可以解决你的问题。

在未来的实现中,您可以直接在可绘制的列表选择器本身中提供启用/禁用的背景资源。使用 state_enabled = true/false。这消除了在代码中执行所有这些操作的需要。

关于java - setEnabled(false) -> (true)/selectableItemBackground 作为可绘制后没有可见的按钮反馈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748290/

相关文章:

java - 构造函数 if 语句

android - 如何避免不同 Android API 版本出现 NoClassDefFoundError

java - 使用相机 API 在 Android 上切换相机

android - 如何在 VMware Workstation 8.0 中运行的 Android 2.2 上安装应用程序?

单击按钮时的 JavaFX 新场景

html - 覆盖按钮上的 Bootstrap 事件/访问类

java - 如何获取最新的共享首选项

java - Portecle 软件从 pem 转换为 bks - 安装错误 NoClassFoundException

java - 如何修复 'Cannot access database on the main thread since it may potentially lock the UI for a long period of time.'

CSS:如何让我的 "send"按钮正确排列(向上移动)和放大(缩放)?