Android RadioButton設置選中時文字和背景顏色同時改變


主要應用在購物車,像淘寶的那樣,點擊以后彈出一個選擇種類顏色這樣的popuwindow以后,然后這個選擇種類的地方要用到類似這個玩意兒。

搜了一下,效果和這個文章一致。轉了。

原文地址:http://blog.csdn.net/liuwan1992/article/details/52688408

在使用 RadioButton 時,有時我們會想要達到選中時文字顏色和背景顏色同時改變的效果,這里還需要多進行幾步操作。

首先,在布局文件中新建一組 RadioButton :

 

[html]  view plain  copy
 
  1. <RadioGroup  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:gravity="center"  
  5.     android:orientation="horizontal">  
  6.   
  7.     <RadioButton  
  8.         android:id="@+id/btn1"  
  9.         android:layout_width="0dp"  
  10.         android:layout_height="35dp"  
  11.         android:layout_weight="1"  
  12.         android:background="@drawable/radiobutton_background"  
  13.         android:button="@null"  
  14.         android:gravity="center"  
  15.         android:text="P0501"  
  16.         android:textColor="@color/radiobutton_textcolor"  
  17.         android:textSize="14sp" />  
  18.   
  19.     <RadioButton  
  20.         android:id="@+id/btn2"  
  21.         android:layout_width="0dp"  
  22.         android:layout_height="35dp"  
  23.         android:layout_marginStart="10dp"  
  24.         android:layout_weight="1"  
  25.         android:background="@drawable/radiobutton_background"  
  26.         android:button="@null"  
  27.         android:gravity="center"  
  28.         android:text="P0502"  
  29.         android:textColor="@color/radiobutton_textcolor"  
  30.         android:textSize="14sp" />  
  31.   
  32.     <RadioButton  
  33.         android:id="@+id/btn3"  
  34.         android:layout_width="0dp"  
  35.         android:layout_height="35dp"  
  36.         android:layout_marginStart="10dp"  
  37.         android:layout_weight="1"  
  38.         android:background="@drawable/radiobutton_background"  
  39.         android:button="@null"  
  40.         android:gravity="center"  
  41.         android:text="P0503"  
  42.         android:textColor="@color/radiobutton_textcolor"  
  43.         android:textSize="14sp" />  
  44.   
  45. </RadioGroup>  

這里面有三個屬性要做一下說明:

 

1、Android:button="@null" 這樣設置可以不顯示我們通常所見的 RadioButton 中的圓形選中按鈕.

2、android:background="@drawable/radiobutton_background" 這里設置了背景選擇器,代碼如下:

 

[html]  view plain  copy
 
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <item android:drawable="@drawable/radiobutton_background_unchecked"  
  3.         android:state_checked="false" />  
  4.     <item android:drawable="@drawable/radiobutton_background_checked"  
  5.         android:state_checked="true" />  
  6. </selector>  

這里面的選中樣式又指向一個 Drawable 資源文件 radiobutton_background_checked.xml ,具體代碼如下:

 

 

[html]  view plain  copy
 
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:shape="rectangle">  
  3.     <!-- 填充 -->  
  4.     <solid android:color="@color/color14" />  
  5.     <!-- 圓角 -->  
  6.     <corners android:radius="5dp" />  
  7. </shape>  

以上這些資源文件都放在 res/drawable/ 目錄下。

 

3、android:textColor="@color/radiobutton_textcolor" 這里設置了字體顏色選擇器,需要稍作說明的是:需要在 res 目錄下新建一個

文件夾取名為 color ,將字體顏色選擇器 radiobutton_textcolor.xml 文件存放在 res/color/ 目錄下面。代碼如下:

 

[html]  view plain  copy
 
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <item android:color="@color/color2"  
  3.         android:state_checked="false" />  
  4.     <item android:color="@color/color1"  
  5.         android:state_checked="true" />  
  6. </selector>  

經過以上步驟后,我們來看一下效果圖:

 

     

最后提一下怎么通過 RadioGroup 獲取 RadioButton :

 

[java]  view plain  copy
 
  1. RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);  
  2. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  3.     @Override  
  4.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  5.         RadioButton radioButton = (RadioButton) group.findViewById(checkedId);  
  6.         String result = radioButton.getText().toString();  
  7.     }  
  8. });  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM