1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android单选按钮_Android单选按钮示例

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

时间:2021-03-29 09:31:35

相关推荐

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

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. ARadioGroupis 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单选按钮和单选组的一些属性:

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 verticalandroid:orientation:单选按钮组上的此属性定义用于定位其子视图(由单选按钮组成)的方向。 它可以是水平或垂直的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 selectioncheck(id):这会将选择设置为单选按钮,其标识符传递到参数中。 -1用作选择标识符以清除选择clearCheck(): It clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns nullclearCheck():清除选择。 清除选择后,该组中没有选中任何单选按钮,并且getCheckedRadioButtonId()返回nullgetCheckedRadioButtonId(): It returns the identifier of the selected radio button in this group. If its empty selection, the returned value is -1getCheckedRadioButtonId():返回该组中所选单选按钮的标识符。 如果选择为空,则返回值为-1setOnCheckedChangeListener(): This registers a callback to be invoked when the checked radio button changes in this group. We must supply instance ofRadioGroup.OnCheckedChangeListenertosetOnCheckedChangeListener()methodsetOnCheckedChangeListener():此方法注册一个回调,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 aRadioGroupcontaining 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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"tools:context="com.journaldev.radiobuttons.MainActivity"><RadioGroupandroid:layout_margin="@dimen/activity_horizontal_margin"android:id="@+id/radioGroup"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButtonandroid:id="@+id/radioButton1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Radio Button 1 "android:textSize="18sp"/><RadioButtonandroid:id="@+id/radioButton2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Radio Button 2"android:textSize="18sp"/><RadioButtonandroid:id="@+id/radioButton3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Radio Button 3"android:textSize="18sp"/></RadioGroup><Buttonandroid: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" /><Buttonandroid: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>

TheMainActivity.javasource 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;@Overrideprotected 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() {@Overridepublic 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 invokingclearCheck()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 invokinggetCheckedRadioButtonId()on the radio group object.

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

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

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

This brings an end to this tutorial. You can download the finalAndroid Radio Button Projectfrom the link below.

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

Download Android Radio Button Example Project下载Android单选按钮示例项目

Reference: Official Documentation

参考: 官方文档

翻译自: /10251/android-radio-button

android单选按钮

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。