中级控件——单选按钮RadioButton——horizontal方向
单选按钮要在一组按钮中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组RadioGroup。
RadioGroup实质上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下。
除了RadioButton,也允许放置其他控件。
单选组与线性布局相比,它们主要有以下两个区别:
(1)单选组多了管理单选按钮的功能,而线性布局不具备该功能;
(2)如果不指定orientation属性,那么单选组默认垂直排列,而线性布局默认水平排列;
判断选中了哪个单选按钮,通常不是监听某个单选按钮,而是监听单选组的选中事件。
下面是RadioGroup常用的3个方法:
(1)、check:选中指定资源编号的单选按钮。
(2)、getCheckedRadioButtonId:获取选中状态单选按钮的资源编号。
(3)、setOnCheckedChangeListener:设置单选按钮勾选变化的监听器。
==================================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择您的性别" android:textColor="@color/black" android:textSize="17sp" /> <RadioGroup android:id="@+id/rg_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_male" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="男" android:textColor="@color/black" android:textSize="17sp" /> <RadioButton android:id="@+id/rb_female" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="女" android:textColor="@color/black" android:textSize="17sp" /> </RadioGroup> <TextView android:id="@+id/tv_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>
package com.example.myapplication; import android.annotation.SuppressLint; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private TextView tv_sex; // 声明一个文本视图对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 从布局文件中获取名叫tv_sex的文本视图 tv_sex = findViewById(R.id.tv_sex); // 从布局文件中获取名叫rg_sex的单选组 RadioGroup rg_sex = findViewById(R.id.rg_sex); // 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法 rg_sex.setOnCheckedChangeListener(this); // 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法 // rg_sex.setOnCheckedChangeListener(new RadioListener()); } // 在用户点击组内的单选按钮时触发 @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.rb_male) { tv_sex.setText("哇哦,你是个帅气的男孩"); } else if (checkedId == R.id.rb_female) { tv_sex.setText("哇哦,你是个漂亮的女孩"); } } // 定义一个单选监听器,它实现了接口RadioGroup.OnCheckedChangeListener // class RadioListener implements RadioGroup.OnCheckedChangeListener // { // // 在用户点击组内的单选按钮时触发 // @Override // public void onCheckedChanged(RadioGroup group, int checkedId) // { // Toast.makeText(MainActivity.this, "您选中了控件"+checkedId, Toast.LENGTH_LONG).show(); // } // } }
==========================================================================================
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
private TextView tv_sex; // 声明一个文本视图对象
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 从布局文件中获取名叫tv_sex的文本视图
tv_sex = findViewById(R.id.tv_sex);
// 从布局文件中获取名叫rg_sex的单选组
RadioGroup rg_sex = findViewById(R.id.rg_sex);
// 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
// rg_sex.setOnCheckedChangeListener(this);
// 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
rg_sex.setOnCheckedChangeListener(new RadioListener());
}
// 定义一个单选监听器,它实现了接口RadioGroup.OnCheckedChangeListener
class RadioListener implements RadioGroup.OnCheckedChangeListener
{
// 在用户点击组内的单选按钮时触发
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Toast.makeText(MainActivity.this, "您选中了控件"+checkedId, Toast.LENGTH_LONG).show();
}
}
}
分类:
工作需要———安卓app学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效