android RadioGroup实现单选以及默认选中
代码下载链接: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实现单选以及默认选中的更多相关文章
- android RadioGroup设置某一个被选中
见码滚 mPriorityRadioGroup.clearCheck(); mStatusRadioGroup.clearCheck(); RadioButton r1 = (RadioButton) ...
- Android RadioGroup中设置默认选中RadioButton 后,选中两个的问题 解决方法
项目中遇到多个RadioGroup中单选RadioButton ,设置了默认选中第一个 . 然后就 能选中两个RadioButton . . .. 我开始这样给设置默认选中一个的: for (int ...
- android RadioGroup中设置selector后出现多个别选中的RadioButton的解决办法
在一个RadioGroup组中假如有三个或者以上的RadioButton,当然你需要给这些RadioButton设置selector.设置其中的一个为默认选中状态(在xml中设置).当程序在手机上运行 ...
- ios开发之--tableview单选/多选实现(非tableview的editing状态)及默认选中
实现思路比较简单,这里仅做记录: 直接上代码: 1,实现didSelectRowAtIndexPath方法 -(void)tableView:(UITableView *)tableView didS ...
- 使用vue如何默认选中单选框
使用了vue以后,发现这真的是一个灵活高效的框架,能够轻松实现页面的实时刷新.那么,今天先聊聊单选框的使用.一般我们使用单选框,会这么写: //HTML <input type=" c ...
- 如何获取select中的value、text、index相关值 && 如何获取单选框中radio值 && 触发事件 && radio 默认选中
如何获取select中的value.text.index相关值 select还是比较常用的一个标签,如何获取其中的内容呢? 如下所示: <select id="select" ...
- thymeleaf单选回显,多选回显,选回显,下拉默认选中第一个
//默认选中第一个<input type ="radio" name="repaymentType" th:each ="repaymentTy ...
- echarts 设置默认选中,单选
默认选中 和 不选中 传送门
- Android RadioGroup和RadioButton详解
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...
随机推荐
- 各种字符编码方式详解及由来(ANSI,UNICODE,UTF-8,GB2312,GBK)
一直对字符的各种编码方式懵懵懂懂,什么ANSI UNICODE UTF-8 GB2312 GBK DBCS UCS……是不是看的很晕,假如您细细的阅读本文你一定可以清晰的理解他们.Let's go! ...
- vue - src for components || router(index.js)
描述:重新编写一个组件 1.1 编写一个PrintName.vue <!--这里是模板 --> <template> <div class="hello&quo ...
- maven 编译指定模块
编译构建指定的模块 mvn clean install -pl model1,model2 -am -Dmaven.test.skip=true
- taro 创建 Tabbar
1.代码 src/app.js import '@tarojs/async-await' import Taro, { Component } from '@tarojs/taro' import H ...
- android源代码分析 android toast使用具体解释 toast自己定义
在安卓开发过程中.toast使我们常常使用的一个类.当我们须要向用户传达一些信息,可是不须要和用户交互时,该方式就是一种十分恰当的途径. 我们习惯了这样使用toast:Toast.makeText(C ...
- Servlet容器 Jetty
http://www.oschina.net/p/jettyJetty 是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境.Jetty是使用Java语 ...
- C#调用 oracle存储过程
C#调用oracle 存储过程与调用Sql server存储过程类似,比较简单:直接给出示例: /// <summary> /// 判断物料类型是不是总部管控 /// </summa ...
- VMware安装Centos7超详细过程(图文)
1.软硬件准备 软件:推荐使用VMwear,我用的是VMwear 12 镜像:CentOS6 ,如果没有镜像可以在阿里云下载 https://mirrors.aliyun.com/centos/ 硬件 ...
- openstack neutron L3 HA
作者:Liping Mao 发表于:2014-08-20 版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 近期Assaf Muller写了一篇关于Neutro ...
- wx小程序的学习
传送门: # 微信小程序案例大全 https://www.cnblogs.com/icyhoo/p/6282574.html # 微信小程序开发工具 https://mp.weixin.qq.com/ ...