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 ...
随机推荐
- Eleven puzzle_hdu_3095(双向广搜).java
Eleven puzzle Time Limit: 20000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Unity AssetServer小笔记
普及一下Asset Server知识: Unity的Asset Server,其实就是一个纯PostgreSQL数据库.Unity操作Asset Server,就是一个数据库客户端操作,但是Unity ...
- Customize User Interfaces and Pass User Input to Installer Classes
In this article I am going to demonstrate how to customize your MSI install to prompt the user for s ...
- html热点区域
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C++中返回对象的情形及RVO
http://www.cnblogs.com/xkfz007/archive/2012/07/21/2602110.html 之前有文章介绍过临时对象和返回值优化RVO方面的问题.见此处. 在C++中 ...
- 为 sublime text3 添加 github 上的插件
1.CMD+SHIFT+P ---> ADD REPOSITORY 输入 github 上的地址 https://github.com/akira-cn/sublime-gbk 2.CMD+SH ...
- classloader常见问题总结
h tp://yourenyouyu2008.iteye.com/blog/779707看到一些ClassNoFindException ,ClassCastException等异常首先应该想到是不是 ...
- 阿里云web播放器
原文地址:https://help.aliyun.com/document_detail/51991.html?spm=5176.doc61109.6.703.ZTCYoi 一.概念说明 1. pla ...
- android 模拟器上传文件 Read-only file system
在cmd窗口一条命令就可以了:adb shellmount -o remount rw /
- 两种方法一句代码隐藏Activity的标题栏
把Activity的标题栏隐藏有两种方法.一种是在在Activity里面设置javacode.还有一种是在项目的清单文件AndroidManifest.xml中设置模版样式. 一.在Activity中 ...