Kotlin中按钮状态的不同背景 [英] Different background for a button's states in Kotlin
问题描述
我的项目中有一个按钮,其工作方式如下:
I have a button in my project that works like this:
每种状态下按钮都有3种不同的设计-禁用( state_enabled ="false"
),启用,按下.
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>
我想拥有的是:
-
已禁用:-表示除非选择了文件,否则无法单击它.
Disabled: - means I can't click on it unless I have a file selected.
已启用:-表示我已选择一个文件.
Enabled: - means I have selected a file.
按下:-表示是否启用了该按钮并且我按下了该按钮.
Pressed: - means if the button is enabled and I press on it.
state_enabled ="false"
和 state_enabled ="true"
正常工作,而 state_pressed ="true"
不适用于全部.您认为我做错了什么?
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!
推荐答案
您应该为此设置按钮 clickable
.
您可以使用 button.setClickable(true)
(如果您使用的是JAVA)来制作它,也可以在 .xml
中添加 android:clickable ="true"
.
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"
.
如果您使用的是Kotlin,请添加 button.clickable = true
,而不是第一个使用Java代码的解决方案.
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..!
这篇关于Kotlin中按钮状态的不同背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!