自定义单选按钮的图标

10

我正在尝试为Android中的单选按钮实现自定义图标。也就是说,我希望单选按钮图标是我选择的自定义图标之一。我能够通过单选按钮内的android:button属性来实现这一点,但我不知道如何在用户点击它时定义它。在我的应用程序中,当用户单击它时什么都没有发生。我还遵循了这里的指南。

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>

请问有人可以指导我吗?
3个回答

26

右键单击drawable文件夹,然后->新建->Android xml文件->选择根元素"selector",然后将下面的代码粘贴到文件中:

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

然后在您的单选按钮的XML中添加这一行:

android:button="@drawable/radio"

在这里,radio是我们在drawable文件夹中创建的选择器xml文件。


7

//首先在drawable文件夹中创建名为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="@drawable/custom_radio_search_location"
        android:checked="true"
        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="@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="@drawable/custom_radio_search_location"
        android:text="C"
        android:textColor="@android:color/black" />
</RadioGroup>

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>

我在我的XML文件中有多个单选按钮。这将仅对一个单选按钮生效。 - TechFrk
有两个按钮,button_female和button_male。请发布您的代码。 - Karthika PB
你为什么给单选按钮提供文本?如果你提供了android:background="@color/color"和android:button="@drawable/yourpic",你可以简单地为单选按钮提供图片表示,并检查radiogroup的onchecked change。 - Karthika PB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接