Android单选和多选按钮的使用


单项选择RadioButton和多项选择CheckBox的使用


前言:

在上一章我写了关于安卓UI控件之Button,这一章就来讲解下它的子类RadioButton和CheckBox。

在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果。而多项选择则是通过CheckBox来实现的。


1、单选选择RadioButton

我们知道,一个单项选择是由两部分组成的,分别是前面的选择按钮和后面的“答案”。

选择按钮可以通过RadioButton来实现,而“答案”则可以通过RadioGroup来实现。

  具体的实现步骤如下:

  首先,在布局文件中定义一个TextView控件,用来显示问题。

  然后,再在布局文件中定义一个RadioGroup控件,用来显示答案。

  最后,再在RadioGroup控件中定义四个(根据需求而定)RadioButton控件,并将“答案”分别赋给每个选项。

  

如下是单项选择的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--题目-->
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android内核是基于什么系统?"
        android:textSize="24sp"
        />

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <RadioButton
        android:id="@+id/radiobutton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Windows"
        />
    <RadioButton
        android:id="@+id/radiobutton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Mac os"
        />
    <RadioButton
        android:id="@+id/radiobutton3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Linux"
        />
    <RadioButton
        android:id="@+id/radiobutton4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="嵌入式系统"
        />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:layout_marginTop="50dp"
        android:text="进入下一项测试"
        />
</LinearLayout>

因为是单项选择,所以各个选项之间存在互斥性,即仅能选中其中的某一个选项。

那么如何来确定用户的选择是否正确,并给出相应的提示信息呢?

  要确定用户的选择是否正确,需要知道用户选择的是选项中的哪一项,

这可以通过为RadioGroup设置事件监听器setOnCheckedChangeListener来实现。

通过该事件监听器便可以判断出用户点击的是哪一个RadioButton了。然后,再使用Toast来显示相应的提示信息即可。

用上述方案实现单项选择demo的java源代码如下:

<
  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio中实现单选对话框或多选对话框,可以使用AlertDialog.Builder类。下面是实现单选对话框和多选对话框的代码示例: 单选对话框: ```java AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("选择项"); builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理选中项的逻辑 } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理确定按钮点击事件的逻辑 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理取消按钮点击事件的逻辑 } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 多选对话框: ```java AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("选择项"); builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // 处理选中项的逻辑 } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理确定按钮点击事件的逻辑 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理取消按钮点击事件的逻辑 } }); AlertDialog dialog = builder.create(); dialog.show(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值