【问题标题】:No visible button feedback after setEnabled(false) -> (true)/ selectableItemBackground as drawable?setEnabled(false) -> (true)/selectableItemBackground 作为可绘制后没有可见的按钮反馈?
【发布时间】:2013-05-25 23:16:15
【问题描述】:

通过一个按钮,我使用 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" />

【问题讨论】:

  • 输入适当的代码
  • 我添加了代码,但不知道那能告诉你什么,然后我已经写了^^
  • 您确定正在调用 stopSleeping() 吗?
  • 100% 确定。没有其他方法,setBackground... 也可见调用。

标签: java android button


【解决方案1】:

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

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

【讨论】:

  • 听起来很有道理,但我如何重新应用背景可绘制对象?
  • 在你的停止睡眠功能而不是 setBackgroundColor... 做setBackgroundDrawable(getResources().getDrawable(R.drawable.yourDrawableName));
  • bevor 你回答我试过了: buttonDrink.setBackgroundColor(android.R.attr.selectableItemBackground);但与将其设置为透明具有相同的效果。但我认为你误会了我的意思。我不使用可绘制的按钮。
  • 所以你的意思是你使用按钮作为股票......只有在禁用它们时你才添加背景颜色?如果您使用的是普通按钮,则默认情况下可绘制集已经具有用于禁用视图的内置可绘制对象。因此,您也不需要在禁用后设置背景颜色。但是,如果您必须在禁用时更改它,那么您必须在启用时添加可绘制的股票。 setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default);
  • 不完全是库存。我改变了一些东西(我不知道了),这样按钮就没有周围了。我已经用 btn_default 设置了 backgroundDrawable,是的,有一个触摸反馈。但它看起来像我原来的按钮上的一个按钮。哦,伙计,我为什么不记录我用这个按钮做了什么......:/但是谢谢你无论如何都试图帮助我...... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
相关资源
最近更新 更多