Different background for a button's states in Kotlin

浪子不回头ぞ 提交于 2021-01-27 19:19:00

问题


I have a button in my project that works like this:

The button has 3 different designs for each state - disabled (state_enabled="false"), enabled, pressed.

This button remains disabled if no file is selected, and has a particular design for it. Although, when file is selected, this button becomes enabled and switches to a different design. And the button's highlight color is possible to see every time when the button is enabled and pressed.

What I have tried so far:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/background_blue" android:state_enabled="true" />
    <item android:drawable="@drawable/background_blue_white" android:state_enabled="false" />
    <item android:drawable="@drawable/background_green" android:state_pressed="true" />

</selector>

What I want to have is:

  1. Disabled: - means I can't click on it unless I have a file selected.

  2. Enabled: - means I have selected a file.

  3. Pressed: - means if the button is enabled and I press on it.

state_enabled="false" and state_enabled="true" work just fine, whereas state_pressed="true" doesn't work at all. What do you think I am doing wrong?

Please, let me know if my explanations are complicated - I will do my best to describe the problem to make it be as understandable as possible. Thank you so much. Have a nice day!


回答1:


You should make your button clickable for that.

You can make it with button.setClickable(true)(If you are using JAVA) or you can add in your .xml where you have android:clickable="true".

If you are using Kotlin then add button.clickable = true instead of the first solution with Java code.

Update: You should try a this thing as well. Follow this answer might be helpful. As per that answer, you should keep all states into proper order. I don't know why but I think it should help.

Try this and let me know if it will help you. Thanks & Happy coding..!




回答2:


Maybe you should set a default color.

<item android:drawable="@drawable/background_blue" android:state_enabled="true" />
<item android:drawable="@drawable/background_blue_white" android:state_enabled="false" />
<item android:drawable="@drawable/background_green" android:state_pressed="true" />
<item android:drawable="@drawable/background_blue" />

The default color without any "pressed" and "enabled" drawable.




回答3:


A selector will select the first item that matches the current state.

From the documentation:

Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).

A pressed button is also enabled, so your selector still picks the 'enabled' state over the 'pressed' state because it was defined earlier.

You can try adjusting the ordering to fix your issue:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/background_green" android:state_pressed="true" />  <!-- pressed -->
    <item android:drawable="@drawable/background_blue_white" android:state_enabled="false" />  <!-- disabled -->
    <item android:drawable="@drawable/background_blue" />  <!-- default -->

</selector>

To avoid confusion like this in the future a good approach is to be more specific such so that only one item matches at a time.
These items use the original order but only one will match at a time:

    <item android:drawable="@drawable/background_blue" android:state_enabled="true" android:state_pressed="false" />
    <item android:drawable="@drawable/background_blue_white" android:state_enabled="false" android:state_pressed="false"  />
    <item android:drawable="@drawable/background_green" android:state_enabled="true" android:state_pressed="true" />

(Note that it would be better to have a default at the bottom because now it would technically be possible that none match)



来源:https://stackoverflow.com/questions/62038945/different-background-for-a-buttons-states-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!