[英]Button not changing color onClick
I think I've got the required stuff. 我想我已经掌握了必要的东西。 I want to change the color of my button on state_pressed. 我想更改state_pressed上按钮的颜色。 I've got the following resources: 我有以下资源:
color.xml in values with the following content: color.xml中包含以下内容的值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#045FB4</color>
<color name="clicked">#A9E2F3</color>
</resources>
button_dashboard in the drawable folder with the following content: 可绘制文件夹中的button_dashboard包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" />
<item android:drawable="@color/clicked" android:state_pressed="true"/>
</selector>
And finally this Button in my layout: 最后是我布局中的这个Button:
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/button_dashboard"
android:text="Button" />
Am I doing something wrong? 难道我做错了什么? I haven't written any onClick code yet because that's not required at this point (just experimenting). 我还没有编写任何onClick代码,因为此时不需要(只需进行实验)。
Try: 尝试:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/clicked" android:state_pressed="true"/>
<item android:drawable="@color/blue" />
</selector>
Change content of button_dashboard in the drawable folder to this 将drawable文件夹中button_dashboard的内容更改为此
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="@color/clicked" />
</shape>
</item>
<item>
<shape>
<solid
android:color="@color/blue" />
</shape>
</item>
</selector>
And finally this Button in your layout: 最后是您布局中的这个Button:
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/button_dashboard"
android:text="Button" />
Hope this helps.. 希望这可以帮助..
use this one drawable/selector.xml 使用这个drawable / selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@color/Red"/>
<item android:state_pressed="true" android:drawable="@color/Red" />
<item android:drawable="@color/White" />
</selector>
and set as background of button in oncreate method 并在oncreate方法中设置为按钮的背景
btn.setBackgroundResource(R.drawable.selector);
Add the following line to your button layout: 将以下行添加到您的按钮布局:
android:clickable="true"
So the button in your layout should now be: 因此,布局中的按钮现在应为:
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:clickable="true"
android:background="@drawable/button_dashboard"
android:text="Button" />
Add one more line to your button_dashboard 在您的button_dashboard上再添加一行
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/clicked" android:state_pressed="true"/>
<item android:drawable="@color/clicked" android:state_selected="true"/>
<item android:drawable="@color/blue" />
</selector>
After this please add one line code to your button click listener 之后,请在您的按钮点击监听器中添加一行代码
button2.setSelected(true);
Hope this will help you. 希望这会帮助你。
how about this? 这个怎么样?
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="@color/clicked" />
</item>
<item android:state_pressed="false">
<color android:color="@color/blue" />
</item>
</selector>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.