Dodawanie niestandardowych przycisków radiowych w systemie android

Próbuję uzyskać efekt radiobutton dla zwykłych przycisków w Androidzie

Mam prosty przycisk radiowy Androida poniżej

Tutaj wpisz opis obrazka

Kod do tego to ::

Activity_main.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=".MainActivity" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton1" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton2" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton3" />
    </RadioGroup>


</RelativeLayout>

Jak dostosować go jak poniżej::

Tutaj wpisz opis obrazka

Dzięki !

[edytuj] użycie kodu z jednej z odpowiedzi

Tutaj wpisz opis obrazka

Ale nazwa przycisku jest przyćmiona przez opcję Wybierz, jak go usunąć ?


{edytuj} więcej zmian

Na koniec powinienem wiedzieć, który przycisk wybrałem z trzech przycisków radiowych .... czy można uzyskać jak poniżej ?

Tutaj wpisz opis obrazka

 88
Author: Devrath, 2013-10-03

9 answers

Dodaj rysowalne tło, które odnosi się do obrazu lub selektora (jak poniżej), i ustaw przycisk przezroczysty:

<RadioButton
    android:id="@+id/radio0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/yourbuttonbackground"
    android:button="@android:color/transparent"
    android:checked="true"
    android:text="RadioButton1" />

Jeśli chcesz, aby Twoje przyciski radiowe miały inny zasób po zaznaczeniu, Utwórz rysowalne tło selektora:

res/drawable / yourbuttonbackground.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/b"
        android:state_checked="true"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/a"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/a"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/b" />
</selector>

W powyższym selektorze odwołujemy się do dwóch drawables, a i b, Oto jak tworzymy them:

res/drawable/a.xml - wybrany stan

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners
        android:radius="5dp" />
    <solid
        android:color="#fff" />
    <stroke
        android:width="2dp"
        android:color="#53aade" />
</shape>

res/drawable/b.xml - Stan regularny

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners
        android:radius="5dp" />
    <solid
        android:color="#fff" />
    <stroke
        android:width="2dp"
        android:color="#555555" />
</shape>

Więcej o losowaniu tutaj: http://developer.android.com/guide/topics/resources/drawable-resource.html

 188
Author: Evan Bashir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 17:40:10

Użyj tego samego formatu pliku XML z odpowiedzi Evana, ale jeden plik rysowalny to wszystko, czego potrzebujesz do formatowania.

<RadioButton
    android:id="@+id/radio0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button_background"
    android:button="@android:color/transparent"
    android:checked="true"
    android:text="RadioButton1" />

I twój osobny plik:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_pressed="true" >
         <shape android:shape="rectangle" >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="#333333" />
             <solid android:color="#cccccc" />            
         </shape>
    </item>

    <item android:state_checked="true">
         <shape android:shape="rectangle" >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="#333333" />
             <solid android:color="#cccccc" /> 
         </shape>
    </item>  

    <item>
         <shape android:shape="rectangle"  >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="#cccccc" />
             <solid android:color="#ffffff" />            
         </shape>
    </item>
</selector>
 33
Author: Seslyn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-18 10:38:40

Musisz wypełnić atrybut "Button" klasy "CompoundButton" rysowalną ścieżką XML (my_checkbox ). W pliku XML drawable musisz mieć :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="false" android:drawable="@drawable/checkbox_not_checked" />
     <item android:state_checked="true" android:drawable="@drawable/checkbox_checked" />
     <item android:drawable="@drawable/checkbox_not_checked" /> <!-- default -->
</selector>

Nie zapomnij zastąpić my_checkbox przez nazwę pliku pola wyboru drawable , checkbox_not_checkedprzez twój PNG drawable, który jest Twoim pole wyboru, gdy nie jest zaznaczone i checkbox_checked twoim obrazem, gdy jest zaznaczone.

Dla rozmiaru, bezpośrednio zaktualizuj układ parametry.

 8
Author: flchaux,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 16:11:49

Aby ukryć domyślny przycisk radiowy, sugeruję usunąć przycisk zamiast uczynić go przezroczystym, ponieważ wszystkie informacje zwrotne są obsługiwane przez rysowalne tło:

android:button="@null"

Również lepiej byłoby użyć stylów , ponieważ istnieje kilka przycisków radiowych:

<RadioButton style="@style/RadioButtonStyle" ... />

<style name="RadioButtonStyle" parent="@android:style/Widget.CompoundButton">
    <item name="android:background">@drawable/customButtonBackground</item>
    <item name="android:button">@null</item>
</style>

Będziesz potrzebował Seslyn customButtonBackground drawable .

 4
Author: McX,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:22

Najlepszym sposobem na dodanie niestandardowego drawable jest:

 <RadioButton
                                android:id="@+id/radiocar"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:background="@android:color/transparent"
                                android:button="@drawable/yourbuttonbackground"
                                android:checked="true"
                                android:drawableRight="@mipmap/car"
                                android:paddingLeft="5dp"
                                android:paddingRight="5dp"
                                android:text="yourtexthere"/>

Nakładka Cień przez niestandardowe drawable jest usuwana tutaj.

 4
Author: Abhilasha Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-10 04:32:54

Simple way try this

  1. Utwórz nowy układ w folderze drawable i nadaj mu nazwę custom_radiobutton (możesz również zmienić nazwę)

     <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_checked="false" 
    android:drawable="@drawable/your_radio_off_image_name" />
     <item android:state_checked="true" 
    android:drawable="@drawable/your_radio_on_image_name" />
    </selector>
    
  2. Użyj tego w aktywności układu

    <RadioButton
     android:id="@+id/radiobutton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:button="@drawable/custom_radiobutton"/>
    
 3
Author: Sunil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-03 07:27:38

Poniżej znajduje się przykład niestandardowego przycisku radiowego. wykonaj poniższe czynności..

  1. Plik Xml.

                        <FrameLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.5">
    
    
                            <TextView
                                android:id="@+id/text_gender"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="left|center_vertical"
                                android:gravity="center"
                                android:text="@string/gender"
                                android:textColor="#263238"
                                android:textSize="15sp"
                                android:textStyle="normal"
    
                                />
    
                            <TextView
                                android:id="@+id/text_male"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginLeft="10dp"
                                android:gravity="center"
                                android:text="@string/male"
                                android:textColor="#263238"
                                android:textSize="15sp"
                                android:textStyle="normal"/>
    
                            <RadioButton
                                android:id="@+id/radio_Male"
                                android:layout_width="28dp"
                                android:layout_height="28dp"
                                android:layout_gravity="right|center_vertical"
                                android:layout_marginRight="4dp"
                                android:button="@drawable/custom_radio_button"
                                android:checked="true"
                                android:text=""
                                android:onClick="onButtonClicked"
                                android:textSize="15sp"
                                android:textStyle="normal"
    
                                />
                        </FrameLayout>
    
                        <FrameLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.6">
    
                            <RadioButton
                                android:id="@+id/radio_Female"
                                android:layout_width="28dp"
                                android:layout_height="28dp"
                                android:layout_gravity="right|center_vertical"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="0dp"
                                android:button="@drawable/custom_female_button"
                                android:text=""
                                android:onClick="onButtonClicked"
                                android:textSize="15sp"
                                android:textStyle="normal"/>
    
                            <TextView
                                android:id="@+id/text_female"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="left|center_vertical"
                                android:gravity="center"
                                android:text="@string/female"
                                android:textColor="#263238"
                                android:textSize="15sp"
                                android:textStyle="normal"/>
    
                            <RadioButton
                                android:id="@+id/radio_Other"
                                android:layout_width="28dp"
                                android:layout_height="28dp"
                                android:layout_gravity="center_horizontal|bottom"
                                android:layout_marginRight="10dp"
                                android:button="@drawable/custom_other_button"
                                android:text=""
                                android:onClick="onButtonClicked"
                                android:textSize="15sp"
                                android:textStyle="normal"/>
    
                            <TextView
                                android:id="@+id/text_other"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="right|center_vertical"
                                android:layout_marginRight="34dp"
                                android:gravity="center"
                                android:text="@string/other"
                                android:textColor="#263238"
                                android:textSize="15sp"
                                android:textStyle="normal"/>
                        </FrameLayout>
                    </LinearLayout>
    

    2.Dodaj niestandardowy xml dla przycisków radiowych

    2.1.inne drawable

    Custom_other_button.xml

    <item android:state_checked="true" android:drawable="@drawable/select_radio_other" />
    <item android:state_checked="false" android:drawable="@drawable/default_radio" />
    

    2.2.żeński drawable

    Custom_female_button.xml

    <item android:state_checked="true" android:drawable="@drawable/select_radio_female" />
    <item android:state_checked="false" android:drawable="@drawable/default_radio" />
    

    2.3. mężczyźni drawable

    Custom_radio_button.xml

    <item android:state_checked="true" android:drawable="@drawable/select_radio_male" />
    <item android:state_checked="false" android:drawable="@drawable/default_radio" />
    

    1. wyjście: to jest ekran wyjściowy
 1
Author: sachin pangare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-19 19:30:05

Zdaję sobie sprawę, że to spóźniona odpowiedź, ale patrząc przez developer.android.com, wydaje się, że przycisk przełączania byłby idealny dla twojej sytuacji.

Http://developer.android.com/guide/topics/ui/controls/togglebutton.html

I oczywiście nadal możesz korzystać z innych sugestii dotyczących rysowania tła, aby uzyskać niestandardowy wygląd.

<ToggleButton 
    android:id="@+id/togglebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button_background"
    android:textOn="On"
    android:textOff="Off"
    />

Teraz, jeśli chcesz przejść do ostatecznej edycji i mieć efekt "halo" wokół przycisków, możesz użyć kolejny selektor Niestandardowy, aby to zrobić.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" > <!-- selected -->
        <shape>
            <solid
                android:color="@android:color/white" />
            <stroke
                android:width="3px"
                android:color="@android:color/holo_blue_bright" />
            <corners
                android:radius="5dp" />
        </shape>
    </item>
    <item> <!-- default -->
        <shape>
            <solid
                android:color="@android:color/white" />
            <stroke
                android:width="1px"
                android:color="@android:color/darker_gray" />
            <corners
                android:radius="5dp" />
        </shape>
    </item> 
</selector>
 0
Author: Arthulia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-18 10:39:15

Ustawienie android:background i android:button Radiobuttona tak, jakby zaakceptowana odpowiedź nie zadziałała dla mnie. Rysowalny obraz był wyświetlany jako tło (mimo że android:button był ustawiony na przezroczysty ) do tekstu przycisku radiowego jako Tutaj wpisz opis obrazka

android:background="@drawable/radiobuttonstyle"
    android:button="@android:color/transparent"

Więc dał radiobutton jako custom drawable radiobuttonstyle.xml

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Maintenance"
    android:id="@+id/radioButton1"
    android:button="@drawable/radiobuttonstyle"
      />

I radiobuttonstyle.xml jest następujący

<?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/ic_radio_checked"></item>
  <item android:state_checked="false" android:drawable="@drawable/ic_radio_unchecked"></item>
</selector>

I po tym radiobutton z niestandardowym stylu przycisku działał.

Tutaj wpisz opis obrazka

 0
Author: Annu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-28 05:47:45