android单选按钮_Android单选按钮示例

本文介绍了Android应用程序中单选按钮的使用,包括其属性、示例代码和项目结构。单选按钮通常用于从列表中选择一个选项,具有选中和未选中两种状态。RadioGroup用于组织单选按钮,可以设置方向并处理单选按钮状态的改变。通过示例代码,展示了如何创建和管理单选按钮的选择状态,并提供了实际应用的输出示例。

android单选按钮

Radio Button in android apps are very common. In this tutorial we’ll implement android radio button widget in our application. Radio Buttons are used when we need to select only one item from a list of presented items.

android应用中的单选按钮非常常见。 在本教程中,我们将在应用程序中实现android单选按钮小部件。 当我们只需要从显示的项目列表中选择一个项目时,使用单选按钮。

Android单选按钮 (Android Radio Button)

A radio button consists of two states – checked and unchecked. Clicking an unchecked button changes its state to “checked” state and “unchecked” for the previously selected radio button. To toggle a checked state to unchecked state, we need to chose another item. Clicking a checked state button doesn’t do any good. A RadioGroup is a set of radio buttons. Radio Buttons that have different parent ViewGroup can’t be termed as a RadioGroup.

单选按钮包含两个状态-选中和未选中。 单击未选中的按钮,将其状态更改为“选中”状态,将先前选择的单选按钮的状态更改为“未选中”。 要将选中状态切换为未选中状态,我们需要选择另一项。 单击选中的状态按钮没有任何好处。 RadioGroup是一组单选按钮。 具有不同父ViewGroup的单选按钮不能称为RadioGroup。

Android单选按钮属性 (Android Radio Button attributes)

Some of the attributes of android radio button and radio group are listed below:

下面列出了android单选按钮和单选组的一些属性:

  1. android:orientation : This property on the Radio group defines the orientation to position its child view consisting of Radio Buttons. It can be either horizontal or vertical

    android:orientation :单选按钮组上的此属性定义用于定位其子视图(由单选按钮组成)的方向。 它可以是水平或垂直的
  2. check(id) : This sets the selection to the radio button whose identifier is passed in parameter. -1 is used as the selection identifier to clear the selection

    check(id) :这会将选择设置为单选按钮,其标识符传递到参数中。 -1用作选择标识符以清除选择
  3. clearCheck() : It clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null

    clearCheck() :清除选择。 清除选择后,该组中没有选中任何单选按钮,并且getCheckedRadioButtonId()返回null
  4. getCheckedRadioButtonId() : It returns the identifier of the selected radio button in this group. If its empty selection, the returned value is -1

    getCheckedRadioButtonId() :返回该组中所选单选按钮的标识符。 如果选择为空,则返回值为-1
  5. setOnCheckedChangeListener() : This registers a callback to be invoked when the checked radio button changes in this group. We must supply instance of RadioGroup.OnCheckedChangeListener to setOnCheckedChangeListener() method

    setOnCheckedChangeListener() :此方法注册一个回调, setOnCheckedChangeListener()的单选按钮在该组中更改时将调用该回调。 我们必须将RadioGroup.OnCheckedChangeListener实例提供给setOnCheckedChangeListener()方法

单选按钮Android应用示例 (Radio Button Android App Example)

Let’s jump onto the implementation of radio button in android application.

让我们跳到android应用程序中单选按钮的实现。

Android单选按钮示例项目结构 (Android Radio Button Example Project Structure)

Android单选按钮示例代码 (Android Radio Button Example Code)

The activity_main.xml consists of a RadioGroup containing three radio buttons and two other buttons to clear the checked states and submit the current checked state.

activity_main.xml由一个RadioGroup组成,该RadioGroup包含三个单选按钮和两个其他按钮,用于清除选中的状态并提交当前选中的状态。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="@android:color/white"
    tools:context="com.journaldev.radiobuttons.MainActivity">


    <RadioGroup
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Radio Button 1 "
            android:textSize="18sp"/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Radio Button 2"
            android:textSize="18sp"/>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Radio Button 3"
            android:textSize="18sp"/>
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLEAR"
        android:id="@+id/button"
        android:onClick="onClear"
        android:textSize="18sp"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/radioGroup"
        android:layout_alignStart="@+id/radioGroup" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBMIT"
        android:onClick="onSubmit"
        android:id="@+id/button2"
        android:textSize="18sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

The MainActivity.java source code is given below:

MainActivity.java源代码如下:

package com.journaldev.radiobuttons;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.clearCheck();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                if (null != rb && checkedId > -1) {
                    Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    public void onClear(View v) {
        /* Clears all selected radio buttons to default */
        radioGroup.clearCheck();
    }

    public void onSubmit(View v) {
        RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
    }

}

As it’s seen in the code, the RadioGroup check is cleared initially by invoking clearCheck() on the RadioGroup object.

从代码中可以看出,通过在RadioGroup对象上调用clearCheck()首先清除RadioGroup检查。

A Toast is displayed whenever the checked state is changed to a new Radio Button. The toast displays the text attached to that Radio Button.

只要将检查状态更改为新的单选按钮,就会显示Toast 。 祝酒词显示附加到该单选按钮的文本。

Clicking the submit button retrieves the id of the current checked RadioButton by invoking getCheckedRadioButtonId() on the radio group object.

单击提交按钮,通过在单选组对象上调用getCheckedRadioButtonId()来检索当前选中的RadioButton的ID。

The output of the radio button android project in action is given below:

radio button in android app

运行中的单选按钮android项目的输出如下:

This brings an end to this tutorial. You can download the final Android Radio Button Project from the link below.

本教程到此结束。 您可以从下面的链接下载最终的Android单选按钮项目

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/10251/android-radio-button

android单选按钮

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值