Android单选按钮示例
时间:2020-02-23 14:29:10 来源:igfitidea点击:
android应用中的单选按钮非常常见。
在本教程中,我们将在应用程序中实现android单选按钮小部件。
当我们只需要从显示的项目列表中选择一个项目时,使用单选按钮。
Android单选按钮
单选按钮包含两种状态-选中和未选中。
单击未选中的按钮会将其状态更改为"选中"状态,将先前选择的单选按钮的状态更改为"未选中"。
要将选中状态切换为未选中状态,我们需要选择另一项。
点击选中的状态按钮没有任何用处。
" RadioGroup"是一组单选按钮。
具有不同父ViewGroup的单选按钮不能称为RadioGroup。
Android单选按钮属性
下面列出了android单选按钮和单选组的一些属性:
- android:orientation:单选按钮组上的此属性定义用于定位其子视图(由单选按钮组成)的方向。
它可以是水平或者垂直的 check(id):这将选择设置为单选按钮,其标识符在参数中传递。
-1用作选择标识符以清除选择- clearCheck():清除选择。
清除选择后,该组中没有选中任何单选按钮,并且getCheckedRadioButtonId()返回null - getCheckedRadioButtonId():返回该组中所选单选按钮的标识符。
如果选择为空,则返回值为-1 - setOnCheckedChangeListener():当该组中选中的单选按钮发生更改时,此方法注册一个回调以进行调用。
我们必须向setOnCheckedChangeListener()方法提供RadioGroup.OnCheckedChangeListener的实例。
单选按钮Android应用示例
让我们跳到android应用程序中单选按钮的实现。
Android单选按钮示例代码
activity_main.xml由一个" 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.theitroad.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>
MainActivity.java源代码如下:
package com.theitroad.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();
}
}
从代码中可以看出,通过在RadioGroup对象上调用clearCheck()可以清除RadioGroup检查。
只要将选中的状态更改为新的单选按钮,就会显示Toast。
祝酒词显示附加到该单选按钮的文本。
单击提交按钮,通过在单选组对象上调用getCheckedRadioButtonId()来检索当前选中的RadioButton的ID。

