2015-04-02 204 views
1

我想在android中爲單選按鈕實現自定義圖標。即我希望單選按鈕圖標是我自己選擇的一種。我可以用單選按鈕中的android:button屬性來做到這一點,但當用戶單擊它時,我沒有弄清楚如何定義它。在我的應用程序中,當用戶點擊它時,沒有任何反應。我也遵循這個指南here單選按鈕的自定義圖標

I have 2 images : 
A. tire.png (When the radio button is not clicked) 
B. tireinvert.png (When the radio button is clicked) 

我RadioButton.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.techfrk.customizeradiobutton.MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <RadioGroup 
       android:id="@+id/newexprgrp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

       <RadioButton 
       android:id="@+id/tvllocrbtn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="40dp" 
       android:text="Travelling Expense (Local)" 
       /> 

        <RadioButton 
       android:id="@+id/tvloutrbtn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="15dp" 
       android:text="Travelling Expense (Outstation)" 

       /> 

       <RadioButton 
       android:id="@+id/phnexprbtn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="15dp" 
       android:text="Phone Expense" 

       /> 

        <RadioButton 
       android:id="@+id/miscexprbtn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="15dp" 
       android:text="Misc Expense" 

       /> 
      </RadioGroup> 

</RelativeLayout> 

任何人都可以請指導我?

回答

8

右擊你的文件夾繪製則 - >新建 - > Android的XML文件 - >選擇根元素「選擇」,然後粘貼下面的代碼文件中:

<item android:drawable="@drawable/tire" android:state_checked="true"/> 
    <item android:drawable="@drawable/tireinvert" android:state_checked="false"/> 

然後在單選按鈕的XML加入這一行:

android:button="@drawable/radio" 

這裏拉德IO是我們在繪製文件夾中創建的選擇XML文件

+0

嘿,非常感謝。那做了這個工作。 – TechFrk 2015-04-02 11:47:45

+0

歡迎您... – 2015-04-02 12:12:30

0

試試這個

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:orientation="horizontal" 
    android:layout_marginLeft="10dp" 
    android:id="@+id/segment_img" 

    android:layout_gravity="right|center_vertical" 
    android:checkedButton="@+id/button_add"> 
     <RadioButton 
      android:id="@+id/button_male" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/violetbg" 
      android:button="@drawable/malewhite" /> 

     <RadioButton 
      android:id="@+id/button_female" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:button="@drawable/female" />  
</RadioGroup> 
+0

我在我的XML文件中有多個單選按鈕。這將只有一個單選按鈕纔有效。 – TechFrk 2015-04-02 11:33:07

+0

有兩個按鈕button_female和button_male.please發佈您的代碼,否則 – 2015-04-02 11:35:59

+0

我已更新我的文章。 – TechFrk 2015-04-02 11:36:55

3

//在第一次創建自定義無線電custom_radio_search_location.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/invite_radio_check" /> 
     <item android:state_checked="false" 
      android:drawable="@drawable/invite_radio_uncheck" /> 
    </selector> 

//比使用custom_radio_search_location.xml在佈局

<RadioGroup 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="3" > 

       <RadioButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" 
        android:button="@null" 
        android:checked="true" 
      android:drawableLeft="@drawable/custom_radio_search_location" 
        android:text="A" 
        android:textColor="@android:color/black" 
        /> 

       <RadioButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" 
        android:button="@null" 
      android:drawableLeft="@drawable/custom_radio_search_location" 
        android:text="B" 
        android:textColor="@android:color/black" 
        /> 

       <RadioButton 

        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" 
        android:button="@null" 
      android:drawableLeft="@drawable/custom_radio_search_location" 
        android:text="C" 
        android:textColor="@android:color/black" 
        /> 
      </RadioGroup>