代码下载链接:http://download.csdn.net/detail/a123demi/7511835

本文将通过radiogroup和radiobutton实现组内信息的单选,

当中radiogroup就是将radiobutton进行分组,同一管理和控制

同一时候实现默认选中情况,获取默认值.效果图

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTEyM2RlbWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

详细实比例如以下:

1.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" > <TextView
android:id="@+id/diplay_seleted_item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_selected"/> <RadioGroup
android:id="@+id/sex_rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FF00"
android:orientation="vertical" > <RadioButton
android:id="@+id/man_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/man" /> <RadioButton
android:id="@+id/woman_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/woman" />
</RadioGroup> </LinearLayout>

2.strings.xml

<?

xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">RadioGroupDemo</string>
<string name="please_selected">请选择你的性别:</string>
<string name="action_settings">Settings</string>
<string name="man">男</string>
<string name="woman">女</string> </resources>

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(){ @Override
public void onCheckedChanged(RadioGroup rg, int checkedId) {
// TODO Auto-generated method stub
if(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 RadioGroup实现单选以及默认选中的更多相关文章

  1. android RadioGroup设置某一个被选中

    见码滚 mPriorityRadioGroup.clearCheck(); mStatusRadioGroup.clearCheck(); RadioButton r1 = (RadioButton) ...

  2. Android RadioGroup中设置默认选中RadioButton 后,选中两个的问题 解决方法

    项目中遇到多个RadioGroup中单选RadioButton ,设置了默认选中第一个 . 然后就 能选中两个RadioButton . . .. 我开始这样给设置默认选中一个的: for (int ...

  3. android RadioGroup中设置selector后出现多个别选中的RadioButton的解决办法

    在一个RadioGroup组中假如有三个或者以上的RadioButton,当然你需要给这些RadioButton设置selector.设置其中的一个为默认选中状态(在xml中设置).当程序在手机上运行 ...

  4. ios开发之--tableview单选/多选实现(非tableview的editing状态)及默认选中

    实现思路比较简单,这里仅做记录: 直接上代码: 1,实现didSelectRowAtIndexPath方法 -(void)tableView:(UITableView *)tableView didS ...

  5. 使用vue如何默认选中单选框

    使用了vue以后,发现这真的是一个灵活高效的框架,能够轻松实现页面的实时刷新.那么,今天先聊聊单选框的使用.一般我们使用单选框,会这么写: //HTML <input type=" c ...

  6. 如何获取select中的value、text、index相关值 && 如何获取单选框中radio值 && 触发事件 && radio 默认选中

    如何获取select中的value.text.index相关值 select还是比较常用的一个标签,如何获取其中的内容呢? 如下所示: <select id="select" ...

  7. thymeleaf单选回显,多选回显,选回显,下拉默认选中第一个

    //默认选中第一个<input type ="radio" name="repaymentType" th:each ="repaymentTy ...

  8. echarts 设置默认选中,单选

    默认选中 和 不选中 传送门

  9. Android RadioGroup和RadioButton详解

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...

随机推荐

  1. Discuz常见小问题2-如何在数据库搜索指定关键字

    点击搜索,然后输入关键字,注意要全选或者部分选择表(一个数据库的表),点击右下角的执行   会提示有多少个匹配,可以在浏览器中直接搜索"1个匹配"来定位到哪个数据表找到了匹配项,然 ...

  2. 如何免密码直接登陆win7

    打开“运行”输入NETPLWIZ,并打开搜索到的程序将“要使用本机,用户必须输入用户名和密码”的勾去掉点 确定在弹出的对话框里填上你想要 自动登陆的 用户名和密码

  3. vue - index.html

    描述:通过App.vue绑定#App元素(进行展示).div#id中间写入任何内容都会失效. <!DOCTYPE html> <html> <head> <m ...

  4. 【Python 数据分析】从Mysql数据库取出数据作图分析

    在之前的文章中[爬取天气信息]我们已经将昆明二月份的气温爬取到数据库了,那么现在我们需要对这些数据进行一些分析操作,下面是使用matplotlib对这些数据的一些操作 折线图 首先我们读取数据库中的数 ...

  5. nginx location静态文件配置

    进入nginx安装目录的conf目录下,修改nginx.conf文件,在一个server{}中添加 一个location 部分配置代码如下 root@ubuntu:/usr/local/nginx/c ...

  6. Swashbuckle一个webapi调试利器

    开发weibapi通常会使用postman或者其他模拟http请求的工具去模拟请求.但是这些工具需要自己保存请求地址的url以及参数 每次我都是先查询wiki看参数. 看见同事再用Swashbuckl ...

  7. Calendar类经常用法 日期间的转换 set方法有巨坑

           今天发现项目的工具类方法有个bug,并且还能迷惑你的bug,刚開始也是非常迷惑,由于这个bug之前出现过,可是过了两天就自己好了.今天又出现了.哦对,今天是 2017年3月31日,之 ...

  8. NFS介绍

    一.NFS服务介绍 NFS是 Network File system的缩写 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机 ...

  9. css - 当文本内容长度超出屏幕宽度时,以省略号代替

    <style> .ellipsis{ text-overflow: ellipsis; overflow: hidden; white-space: nowrap; } </styl ...

  10. HTTPS证书申请相关笔记

    申请免费的HTTPS证书相关资料 参考资料: HTTPS 检测 苹果ATS检测 什么是ECC证书? 渠道2: Let's Encrypt 优点 缺点 Let's Encrypt 的是否支持非80,44 ...