【问题标题】:selector for button not working按钮选择器不起作用
【发布时间】:2012-09-11 09:04:15
【问题描述】:

我想在按钮不可点击时更改按钮的背景。我为此使用了选择器,但在按钮仍然不可点击的情况下它不起作用。

这是选择器文件:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"  android:drawable="@drawable/button_lightgrey" />
    <item  android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/button_blue"/> 
    <item android:state_focused="true" android:drawable="@drawable/button_darkgreen"  /> 
    <item  android:drawable="@drawable/button_lightgreen" /> 
</selector> 

这是我使用此选择器文件的按钮:

<Button
                android:id="@+id/PrevButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onPrevButtonClick"
                android:text="Prev" />

请帮助我。所有其他功能都在选择器文件中工作,但只有不可点击的按钮案例不起作用。谢谢

【问题讨论】:

  • 希望对您有所帮助:stackoverflow.com/questions/3738886/…>
  • 先生,我想让选择器在按钮不可点击时工作。其他状态正在工作,但只有这个不工作。
  • 你想要一个禁用状态的选择器吗? “不可点击”是什么意思?
  • 是的先生,我需要禁用状态的选择器。

标签: android


【解决方案1】:

而不是 button.setClickable(false) 做 button.setEnabled(false) 并使用相同的选择器 xml。

【讨论】:

  • Abhijit Chakra,你的 xml 文件是什么?
  • 我发现问题出在 xml 文件 如果它在第一行只有它在其他方面有效它不会我不知道原因。
  • 原因是android取第一个匹配的状态
【解决方案2】:

这可能对你有帮助

我认为您的选择器文件有问题,请尝试如下选择器文件

请根据需要更换图片

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img_btn_repeat_pressed"
        android:state_focused="true"
        android:state_pressed="true" />
    <item android:drawable="@drawable/img_btn_repeat_pressed"
        android:state_focused="false"
        android:state_pressed="true" />
    <item android:drawable="@drawable/img_btn_repeat_pressed" android:state_focused="true" />
    <item android:drawable="@drawable/img_btn_repeat"
        android:state_focused="false"
        android:state_pressed="false" />
</selector>

【讨论】:

  • 根据需要更改 true 、 false 状态
  • 先生,选择器中禁用状态的条件是什么。
【解决方案3】:

只需使用以下selector.xml修改您的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/round_corner_button_sort" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false" android:drawable="@drawable/round_corner_button_sort" />
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/round_corner_button_sort" />

</selector>

希望对你有所帮助。

编辑

根据您的 cmets,您不能使用状态为 false 的按钮选择器(button.setclickable(false);)在这种状态下,您不能使用您的 selector

【讨论】:

  • 先生,我只想要按钮不可点击时的选择器状态。
  • @user1662334 无法点击??不可点击是什么意思
  • 表示我已将按钮的状态禁用为不可点击。 button.setclickable(false);
  • @user1662334 你认为,当一个按钮在使用button.setclickable(false);时被禁用时,选择器会起作用吗?
  • 对不起,先生,但是在这种状态下可能有一些方法可以改变选择器的状态。
猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 2014-01-31
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 2017-11-25
相关资源
最近更新 更多