zl程序教程

您现在的位置是:首页 >  APP

当前栏目

Android开发——RadioButton控件

2023-04-18 14:29:05 时间

一,简介

RadioButton(单选按钮)

如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!先熟悉下如何使用RadioButton,一个简单的性别选择的例子: 另外我们可以为外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平

效果图:

和    CheckBox(复选框)  区别

如题复选框,即可以同时选中多个选项,至于获得选中的值,同样有两种方式: 1.为每个CheckBox添加事件:setOnCheckedChangeListener 2.弄一个按钮,在点击后,对每个checkbox进行判断:isChecked();

以上参考:2.3.5.RadioButton(单选按钮)&Checkbox(复选框) | 菜鸟教程 (runoob.com)https://www.runoob.com/w3cnote/android-tutorial-radiobutton-checkbox.html

二,实例

实现如下,按下按键,背景高亮

1,实现布局文件  .xml

<RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         .......>
        <RadioButton       
          style="@style/cus_radio"
          ......  />
        <RadioButton
         ......   />
        <RadioButton 
         .......  />
        <RadioButton   
         .......  />

 </RadioGroup>

2,style自定义控件样式,定义在style.xml文件中。

 3,以上实现了RadioButton布局和样式设计,的那如何实现点击高亮呢?

其实是在background里面实现的。

<item name="android:background">@drawable/radio_button</item>

4,drawable/radio_button。

radio_button.xml文件放在了在drawable文件下。


 5,radio_button_unselect.xml文件

  6,radio_button_select.xml文件

 

三,总结

drawable