android - 单选按钮的自定义图标

标签 android radio-button custom-component

我正在尝试为 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>

有人可以指导我吗?

最佳答案

右键单击您的可绘制文件夹,然后->新建->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"

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

关于android - 单选按钮的自定义图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29411752/

相关文章:

javascript - 使用 jQuery 更改组中单选按钮的值属性

java - 当选择单选按钮时,RCP 部分打开为脏

delphi - 是否可以向 delphi 组件添加自定义功能,以便在按下特定键时触发?

android - 自定义属性错误 - Android Studio 1.2

java - 在 JSF1.1 中使用带有 DataTable 的自定义组件时出现重复 ID 错误

java - Google map 无法使用计时器删除标记

java - 简单的 TextToSpeech 代码

java - 为什么在 android 上使用 cryptography.io 和 java 完成的 RSA 身份验证不能一起工作?

android在按下搜索按钮@手机时启动用户定义的 Activity

javascript - 使用 javascript 输入文本的值单选框