2015-04-02 1 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" 

здесь рад И.О. является XML-файл селектор, который мы создали в вытяжке папки

+0

Эй, спасибо большое. Это сработало. – TechFrk

+0

ваш прием ... –

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

+0

есть две кнопки button_female и button_male.please отправьте свой код иначе –

+0

Я обновил свое сообщение. – TechFrk

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> 

 Смежные вопросы

  • Нет связанных вопросов^_^