简体   繁体   English

Android如何创建像一排按钮的RadioButtons

[英]Android How To create RadioButtons like a row of Buttons

In My app i have a search option for stores. 在我的应用程序中,我有一个商店搜索选项。 I can do the search with two criteria: By Name and By Distance. 我可以使用两个条件进行搜索:“按名称”和“按距离”。 The user can choose only one of them. 用户只能选择其中之一。 The required "shape" is 所需的“形状”为 搜索布局

The required RadioButtons marked by the red "circle". 所需的单选按钮以红色“圆圈”标记。 How it can be done? 怎么做? Thanks, Eyal. 谢谢,Eyal。

You can change your radiobutton in xml to something like this 您可以将xml中的单选按钮更改为如下所示

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

And for customradiobutton you can add 对于customradiobutton,您可以添加

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

Another better idea would to use ToggleButton instead of RadioButtons 另一个更好的主意是使用ToggleButton而不是RadioButtons

Use ToggleButtons 使用切换按钮

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toggle"
        android:id="@+id/toggleButton"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM