按钮选择器无效

4

当按钮不可点击时,我希望更改按钮的背景。我已经使用了选择器,但在按钮保持不可点击的情况下它无法工作。

这是选择器文件:

<?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" />

请帮我。选择器文件中的所有其他功能都正常,但只有无法点击按钮的情况不起作用。谢谢。


希望这能帮到你:https://dev59.com/nG865IYBdhLWcg3wl_o3 - user1357696
先生,我想在按钮不可点击时使选择器起作用。其他状态都能正常工作,只有这个状态不能正常工作。 - user1662334
你需要一个禁用状态的选择器吗?“unclickable”是什么意思? - Ads
是的先生,我需要禁用状态的选择器。 - user1662334
3个回答

2

不要使用button.setClickable(false),而是使用button.setEnabled(false),并且使用相同的选择器xml文件。


Abhijit Chakra,你的XML文件是什么? - Gaurav Vashisth
1
我发现问题出在xml文件中的<item android:state_enabled="false" android:drawable="@drawable/button_lightgrey" />,如果它只在第一行,它就能正常工作,否则它就不能正常工作,我不知道原因。 - Abhijit Chakra
1
原因是,Android会采用第一个匹配的状态。 - Mithilesh Izardar

0

只需使用以下的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>

希望这能对你有所帮助。 编辑 根据你的评论,你不能使用状态为false的按钮选择器(button.setclickable(false);)。在这种状态下,你无法使用你的selector

先生,我只想要在按钮不可点击时的选择器状态。 - user1662334
@user1662334 不可点击?你指的是什么意思? - Kumar
意思是我已经禁用了按钮的状态,使其无法点击。button.setClickable(false); - user1662334
@user1662334 你认为当使用 button.setclickable(false); 禁用按钮时,选择器还会起作用吗? - Kumar
抱歉先生,但在这种状态下可能有一些改变选择器状态的方法。 - user1662334

0

这可能会对你有所帮助

我认为你的选择器文件存在问题,请尝试使用以下选择器文件

请根据您的需求更改图像

<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状态。 - Ankitkumar Makwana
先生,请问在选择器中禁用状态的条件是什么? - user1662334

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接