android 按钮按下后改变背景

可通过RadioButton 结合selector实现 通过android:state_checked值改变背景

xml布局文件中关键代码:

 <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="30dp"
            android:layout_marginRight="30dp"
            android:orientation="horizontal" > 

      <RadioButton
                android:id="@+id/image_female"
                android:layout_width="80dp"
                android:layout_height="40dp"
                android:layout_marginEnd="2dp"
                android:layout_marginRight="2dp"
                android:button="@null"
                android:background="@drawable/female_sytle"
                android:contentDescription="@string/image_female_description" />

      <RadioButton
                android:id="@+id/image_male"
                android:layout_width="80dp"
                android:layout_height="40dp"
                android:button="@null"
                android:background="@drawable/male_style"
                android:contentDescription="@string/image_male_description" />

 

     </RadioGroup>

female_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true"  android:drawable="@drawable/register_female_checked"></item>
 <item android:state_checked="false" android:drawable="@drawable/register_female"></item>
 <item android:drawable="@drawable/register_female"></item>
</selector>

 

male_sytle.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" >     <item android:state_checked="true" android:drawable="@drawable/register_male_checked"></item>     <item android:state_checked="false" android:drawable="@drawable/register_male"></item>     <item android:drawable="@drawable/register_male"></item>

</selector>

 

 

参考: http://www.eoeandroid.com/forum.php?mod=viewthread&tid=319623&extra=page%3D1&page=1

第一条回答

 

效果图:

技术分享技术分享

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。