question about change the colours of a button when clicked

可紊 提交于 2019-12-25 14:40:33

问题


My purpose is to change the color of a button when click and my codes are

<?xml version="1.0" encoding="utf-8"?>
selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/btn_askering_active" android:state_selected="true" />
    <!-- When not selected, use white-->
    item android:drawable="@drawable/btn_askering" />
</selector>

It works but if i make a small change like below :

xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When not selected, use white-->
    item android:drawable="@drawable/btn_askering" />
    <!-- When selected, use grey -->
    item android:drawable="@drawable/btn_askering_active" android:state_selected="true" />
</selector>

It does not work anymore.... I need some help...Any comments are welcomed here.Thanks


回答1:


I think you need to mention stats in selector like pressed or focused and change image accordingly. Here i have attached sample selector file,have a look and try accordingly.

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


    <item android:state_focused="true" android:state_pressed="false"
        android:drawable="@drawable/estimator_hover_new" />
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/estimator_hover_new" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/estimator_hover_new" />

    <item android:drawable="@drawable/estimator_new" />


</selector>

All The Best....



来源:https://stackoverflow.com/questions/7301029/question-about-change-the-colours-of-a-button-when-clicked

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