Android学习笔记之RadioButton(RadioGroup)

本文详细介绍了Android开发中RadioButton(单选按钮)和RadioGroup(单选组合框)的使用方法。RadioButton是一个双状态按钮,可以在选中和未选中之间切换,通常与RadioGroup配合使用,后者作为容器可以包含多个RadioButton,并确保在同一组内只能有一个被选中。文章还提供了具体的XML布局代码示例和Java活动代码,展示了如何为RadioGroup设置监听器以及响应不同RadioButton的选中状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RadioButton(单选按钮)在Androi发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sex" /> <RadioGroup android:id="@+id/radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="@string/female" ></RadioButton> <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" android:checked="true" ></RadioButton> </RadioGroup> </LinearLayout>


RadioGroupActivity.java

通过控件的ID来得到代表控件的对象

然后为RadioGroup设置监听器

package Android.Activity; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class RadioGroupActivity extends Activity { /** Called when the activity is first created. */ private RadioButton maleButton = null; private RadioButton femaleButton = null; private RadioGroup radiogroup =null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过控件的ID来得到代表控件的对象 maleButton = (RadioButton)findViewById(R.id.male); femaleButton = (RadioButton)findViewById(R.id.female); radiogroup = (RadioGroup)findViewById(R.id.radiogroup); //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同 radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(femaleButton.getId() == checkedId){ Toast.makeText(RadioGroupActivity.this, "female", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId){ Toast.makeText(RadioGroupActivity.this, "male", Toast.LENGTH_SHORT).show(); } } }); } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值