android RadioGroup实现单选以及默认选中
16lz
2021-01-26
代码下载链接:http://download.csdn.net/detail/a123demi/7511835
本文将通过radiogroup和radiobutton实现组内信息的单选,
其中radiogroup就是将radiobutton进行分组,同一管理和控制
同时实现默认选中情况,获取默认值.效果图
具体实例如下:
1.activity_main.xml
2.strings.xml
<?xml version="1.0" encoding="utf-8"?> RadioGroupDemo 请选择你的性别: Settings 男 女
3.MainActivity.java
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); defaultStr = this.getResources().getString(R.string.please_selected); seletedTv = (TextView) this.findViewById(R.id.diplay_seleted_item_tv); sexRg = (RadioGroup) this.findViewById(R.id.sex_rg); manRb = (RadioButton) this.findViewById(R.id.man_rb); womanRb = (RadioButton) this.findViewById(R.id.woman_rb); manRb.setChecked(true); seletedTv.setText(defaultStr + manRb.getText().toString()); sexRg.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup rg, int checkedId) {// TODO Auto-generated method stubif(checkedId == manRb.getId()){seletedTv.setText(defaultStr + manRb.getText().toString());}else if(checkedId == womanRb.getId()){seletedTv.setText(defaultStr + womanRb.getText().toString());}else{seletedTv.setText(defaultStr);}} }); }
更多相关文章
- Android(安卓)TextView 设置多种颜色
- android radioButton 改变圆圈大小
- Android(安卓)购物车UI及逻辑实现
- Android(安卓)快速实现状态栏透明样式
- android UI设计的一些心得与问题解决(无效果图)
- Android(安卓)带checkbox的listView 实现多选,全选,反选,删除
- Android获取通讯录联系人显示在EditText上Demo
- Android(安卓)其他特效展示
- Android自定义RadioGroup