代码下载链接: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. EAS开发

    WAFII中的 数据获取与传输 首先看实例代码: DataAction:function(){ //获取选中所有列的id var selectedIds = waf("#grid" ...

  2. Jenkins高速上手

    http://www.cnblogs.com/puresoul/p/4813551.html .Jenkins下载安装 1.到官网下载jenkins.war包:http://jenkins-ci.or ...

  3. phpMyAdmim和Yii 连接Mysql报错。

    故障: 之前phpMyAdmim和Yii连接Mysql都好着的.某天,同一时候出现例如以下报错: 1.linux下phpMyAdmin 出现 "缺少 mysqli 扩展,请检查 PHP 配置 ...

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

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

  5. SSH限制ip登陆

    linux限制IP访问ssh   在/etc/hosts.allow输入   (其中192.168.10.88是你要允许登陆ssh的ip,或者是一个网段192.168.10.0/24)   sshd: ...

  6. jqplot使用小心得

    这两天做一个项目,需要画饼图,所以在网上搜到jqplot这个插件.下面就说说我对他的简单的使用心得. 先说说我想要的效果:1.我需要修改饼图每个部分的背景色 2.我需要修改饼图里面文本的颜色和字体大小 ...

  7. echart初体验 动态加载数据

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. python selenium --层级定位

    转自:http://www.cnblogs.com/fnng/p/3193955.html 场景: 假如两个控件,他们长的一模样,还都叫“张三”,唯一的不同是一个在北京,一个在上海,那我们就可以通过, ...

  9. GNU--gprof使用总结

    Added macros ACE_USES_GPROF which enables users to use gprof in a multithreaded environment with ACE ...

  10. 简单的刚開始学习的人配置Android SDK+ADT+Eclipse

    1.下载JDK.Android SDK和Eclipse(Eclipse 版本号最好新一些) 注意:下载的SDK最好和后面的ADT配套,否则Eclipse可能会报错. 如:SDK 21.0.1 相应 A ...