Android RadioButton单选框的使用方法
时间:2021-10-02 14:37:20
public class MainActivity extends Activity {
public RadioGroup mRadioGroup1;
public RadioButton mRadio1, mRadio2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRadioGroup1 = (RadioGroup) findViewById(R.id.gendergroup);
mRadio1 = (RadioButton) findViewById(R.id.girl);
mRadio2 = (RadioButton) findViewById(R.id.boy);
mRadioGroup1.setOnCheckedChangeListener(radiogpchange);
}
private RadioGroup.OnCheckedChangeListener radiogpchange = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == mRadio1.getId()) {
Toast.makeText(getApplicationContext(), "女孩", 1).show();
} else if (checkedId == mRadio2.getId()) {
Toast.makeText(getApplicationContext(), "男孩", 1).show();
}
}
};
}
RadioButton:就像是C#中的Radio控件,可以为控件设置Group,每个Group中的项只能选择一项;
<RadioGroup
android:id="@+id/gendergroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/girl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/girl" />
<RadioButton
android:id="@+id/boy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/boy" />
</RadioGroup>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java状态设计模式实现对象状态转换的优雅方式
![](https://img.aspxhome.com/file/2023/5/92565_0s.png)
解决springboot环境切换失效的问题
浅谈Android开发中项目的文件结构及规范化部署建议
SpringBoot之bootstrap和application的区别解读
![](https://img.aspxhome.com/file/2023/5/106145_0s.png)
浅谈Maven的安装及修改为阿里云下载依赖
java开发之读写txt文件操作的实现
![](https://img.aspxhome.com/file/2023/3/59553_0s.png)
如何在Springboot实现拦截器功能
![](https://img.aspxhome.com/file/2023/1/86831_0s.png)
Android RecyclerView添加FootView和HeadView
![](https://img.aspxhome.com/file/2023/5/109195_0s.gif)
C#文件断点续传实现方法
Spring Boot项目@RestController使用重定向redirect方式
Java 实战项目锤炼之IT设备固定资产管理系统的实现流程
![](https://img.aspxhome.com/file/2023/3/125253_0s.jpg)
android 实现按钮浮动在键盘上方的实例代码
android 调用系统的照相机和图库实例详解
SpringBoot数据访问自定义使用Druid数据源的方法
![](https://img.aspxhome.com/file/2023/1/91081_0s.jpg)
利用ssh实现服务器文件上传下载
Spring4如何自定义@Value功能详解
C#实现简单的Login窗口实例
java基础之字符串编码知识点总结
![](https://img.aspxhome.com/file/2023/3/128843_0s.png)
C++异步操作future和aysnc与function和bind
![](https://img.aspxhome.com/file/2023/3/124013_0s.webp)
使用位运算实现网页中的过滤、筛选功能实例
![](https://img.aspxhome.com/file/2023/3/111253_0s.jpg)