发布时间:2025-12-10 19:39:09 浏览次数:9
RadioGroup总结「终于解决」RadioGroup:实现单选功能的控件 一组RadioButton必须放在一个RadioGroup中。意思就是说单选按钮中的值我们可以看作是一个数组也就是这里说的这个数组是RadioGroup数组中的值是RadioButton,在这个数组中我们通常都有且只能选择一个值,而这个值是这个组中唯一的选择(如:男、女),而这里的RadioButton就是这里举例的男、女的意思。RadioButton常用…
RadioGroup:实现单选功能的控件
一组RadioButton必须放在一个RadioGroup中。意思就是说单选按钮中的值我们可以看作是一个数组也就是这里说的这个数组是RadioGroup数组中的值是RadioButton,在这个数组中我们通常都有且只能选择一个值,而这个值是这个组中唯一的选择(如:男、女),而这里的RadioButton就是这里举例的男、女的意思。
RadioButton
常用属性:
android:text 文字
android:checked 是否选中
列如:
<RadioButton
android:id=“@+id/rb_boy”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“男”
android:checked=“true” />
<RadioButton
android:id=“@+id/rb_girl”
android:layout_marginLeft=“10sp”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“女”/>
常用方法:
boolean isChecked() 返回是否被选中
getText() 返回文字
setChecked(boolean checked) 设置是否选中
列如:
1. publicclassMainActivityextendsAppCompatActivity{
2.
3. //定义按钮对象
4. privateRadioButtonmMaleRb;
5. privateRadioButtonmFamaleRb;
6. privateButtonmOkBt;
7. @Override
8. protectedvoidonCreate(BundlesavedInstanceState){
9. super.onCreate(savedInstanceState);
10.setContentView(R.layout.activity_main);
11.initView();
12.}
13.
14.privatevoidinitView(){
15.//初始化按钮对象
16.mMaleRb=(RadioButton)findViewById(R.id.male_rb);
17.mFamaleRb=(RadioButton)findViewById(R.id.famale_rb);
18.mOkBt=(Button)findViewById(R.id.ok_bt);
19.//给按钮对象设置监听器
20.mOkBt.setOnClickListener(newView.OnClickListener(){
21.@Override
22.publicvoidonClick(Viewv){
23.if(mMaleRb.isChecked()){
24.Toast.makeText(MainActivity.this,“性别是:男”,Toast.LENGTH_SHORT).show();
25.}
26.if(mFamaleRb.isChecked()){
27.Toast.makeText(MainActivity.this,“性别是:女”,Toast.LENGTH_SHORT).show();
28.}
29.}
30.});
31.}
32.}
RadioGroup
常用属性:
android:orientation 方向
列如:
<RadioGroup android: android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!
【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…
常用方法:
getCheckedRadioButtonId() 获得被选中的RadioButton的id
setOnCheckedChangeListener(OnCheckedChangeListener) 设置选中状态修改的监听
列如1:
1. publicclassMainActivityextendsAppCompatActivity{
2.
3. //定义按钮对象
4. privateRadioGroupmSexRg;
5. privateButtonmOkBt;
6. @Override
7. protectedvoidonCreate(BundlesavedInstanceState){
8. super.onCreate(savedInstanceState);
9. setContentView(R.layout.activity_main);
10.initView();
11.}
12.
13.privatevoidinitView(){
14.//初始化按钮对象
15.mSexRg=(RadioGroup)findViewById(R.id.sex_rg);
16.mOkBt=(Button)findViewById(R.id.ok_bt);
17.//给按钮对象设置监听器
18.mOkBt.setOnClickListener(newView.OnClickListener(){
19.intsex=mSexRg.getCheckedRadioButtonId();
20.@Override
21.publicvoidonClick(Viewv){
22.if(sex==R.id.male_rb){
23.Toast.makeText(MainActivity.this,“性别是:男”,Toast.LENGTH_SHORT).show();
24.}
25.if(sex==R.id.famale_rb){
26.Toast.makeText(MainActivity.this,“性别是:女”,Toast.LENGTH_SHORT).show();
27.}
28.}
29.});
30.}
31.}
注意:使用.getCheckedRadioButtonId()来获得它的值的时候,我们要给它设置默认的值,否则提示我们的是空指针异常的问题。
列如2:
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(R.id.rb_boy==checkedId) isGender=true; if(R.id.rb_girl==checkedId) isGender=false; }});