单选按钮(RadioButton)
一:RadioButton的相关属性:


1.Activity
//单选按钮
public class RadioButtonActivity extends Activity { private Context context;
private RadioGroup sexRadioGroup;
private RadioButton maleRadioButton;
private RadioButton femaleRadioButton;
private RadioButton sexRadioButton;
private Button submitButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_button); init();
addAction(); } private void init() {
context = this;
sexRadioGroup = (RadioGroup) findViewById(R.id.sexRadioGroupId);
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButtonId);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButtonId);
sexRadioButton = (RadioButton) findViewById(R.id.sexRadioButtonId);
submitButton = (Button) findViewById(R.id.submitButtonId);
} private void addAction() {
// sexRadioGroup.setOnClickListener(l),setOnClickListener:点击的时候触发
// setOnCheckedChangeListener:状态改变的时候触发
// 两者都能实现对CheckBox的状态改变的监听,但一般情况下,用的更多的是setOnCheckedChangeListener。
//因为,当CheckBox的状态不是通过点击事件改变,而是通过其他的方式改变时,比如setCheck(),setOnClickListener无法完成此种情况下的监听。
//OnCheckChangedListener监听CheckBox的状态,无论来自你的onClick事件还是其他。
sexRadioGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.maleRadioButtonId:
Toast.makeText(context, "你选择了\"男\".",
Toast.LENGTH_SHORT).show();
break;
case R.id.femaleRadioButtonId:
Toast.makeText(context, "你选择了\"女\".",
Toast.LENGTH_SHORT).show();
break;
case R.id.sexRadioButtonId:
Toast.makeText(context, "你选择了\"人妖\".",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}); submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String sex = null;
if (maleRadioButton.isChecked()) {
sex = "男";
}
if (femaleRadioButton.isChecked()) {
sex = "女";
}
if (sexRadioButton.isChecked()) {
sex = "人妖";
}
Toast.makeText(context, "你的性别是:" + sex, Toast.LENGTH_SHORT)
.show();
}
});
} }
2.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- 单选按钮页面 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="20sp" /> <RadioGroup
android:id="@+id/sexRadioGroupId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- android:button="@null" 可以自定义图片-->
<RadioButton
android:id="@+id/maleRadioButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" /> <RadioButton
android:id="@+id/femaleRadioButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" /> <RadioButton
android:id="@+id/sexRadioButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人妖" />
</RadioGroup>
</LinearLayout> <Button
android:id="@+id/submitButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="确定"
android:textSize="20sp" /> </LinearLayout>
3.效果图展示:

单选按钮(RadioButton)的更多相关文章
- .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?
.net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法,区别? RadioButton实例及说明: <asp:RadioButton ID=& ...
- Android 单选按钮(RadioButton)和复选框(CheckBox)的使用
1.RadioButton (1)介绍 (2)单选按钮点击事件的用法 (3)RadioButton与RadioGroup配合使用实现单选题功能 (4)xml布局及使用 <?xml version ...
- Android控件-单选按钮RadioButton
RadioGroup单选按钮用法,还是先看效果图 先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择.下面上代码,main.xml: <RadioGroup an ...
- wpf 自定义单选按钮 RadioButton
新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...
- Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用
1.相关简介 RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式! Checkbox是单独使用,本文为了方便放在了RadioGroup中! 2.简单使用 方法 ...
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
单选按钮(RadioButton)和复选框(CheckBox).状态开关按钮(ToggleButton)与开关(Switch)是用户界面中最普通的UI组件,他们都继承了Button类,因此都可直接使用 ...
- CheckBox和RadioButton
多选按钮CheckBox的使用方法和常用的监听器:OnClickListener.OnCheckedChangeListener 在activity_main.xml中使用LinearLayout布局 ...
- Android的RadioButton和checkBox的用法-android学习之旅(十九)
RadioButton和checkBox简介 单选按钮(RadioButton)和复选框(CheckBox)都继承了Button,因此属性的设置和Button差不多,只是加了一个android:che ...
- Spring MVC单选按钮
以下示例显示如何在使用Spring Web MVC框架的表单中使用单选按钮(RadioButton).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Fra ...
- [WinForm]WinForm跨线程UI操作常用控件类大全
前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...
随机推荐
- 数组或者stack
数组 clear1(long long int array[], size_t int size) { ; i < size; i += ) array[i] = ; } li x5, // i ...
- Jenkins 源代码编译
最近一直想写一个关于 Jenkins 管理的 InelliJ 插件,但是尝试很多次总是在登录认证上面失败,各种办法都不起作用,而且官方的文档含糊不清,就动起了从源代码编译在开发环境中进行调试. 废话少 ...
- e的理解
1. e是一个重要的常数,但是我一直不知道,它的真正含义是什么. 它不像π.大家都知道,π代表了圆的周长与直径之比3.14159,可是如果我问你,e代表了什么.你能回答吗? 维基百科说: " ...
- android4.0 4.1 4.2 4.3 4.4新特性
http://blog.csdn.net/kaiyang45/article/details/7179349 4.0 http://digi.tech.qq.com/a/20120628/000827 ...
- ubuntu下终端路径显示的修改
环境:ubuntu16.04 ubuntu在默认情况下是显示绝对路径的,进入目录过长的时候让人感觉很不舒服,现在修改成只显示当前目录 vim ~/.bashrc 找到这句 # If this is a ...
- 理解Python语言里的异常(Exception)
Exception is as a sort of structured "super go to".异常是一种结构化的"超级goto". 作为一个数十年如一日 ...
- HDU 2191 悼念512汶川大地震遇难同胞
悼念512汶川大地震遇难同胞 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格 ...
- FocusBI:租房分析可视化(PowerBI网址体验)
微信公众号:FocusBI关注可了解更多的商业智能.数据仓库.数据库开发.爬虫知识及沪深股市数据推送.问题或建议,请关注公众号发送消息留言;如果你觉得FocusBI对你有帮助,欢迎转发朋友圈或在文章末 ...
- Java大法之面向对象
总觉得要写点东西,写写自己对知识的理解,对自己学的东西是否编程自己的了.我在想,如果让自己用自己的语言来解释,什么是面向对象,我可能会愣一下,我问自己什么是面向对象的时候,我想了想,自言自语说:面向对 ...
- SQL Serever学习9——基础查询语句
SQL语言概述 SQL是结构化查询语言(Structure Query Language),1974年提出,1979年被IBM实现,SQL语言已经成为关系型数据库的标准语言. 包括: DDL数据定义语 ...