一: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)的更多相关文章

  1. .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?

    .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法,区别? RadioButton实例及说明: <asp:RadioButton ID=& ...

  2. Android 单选按钮(RadioButton)和复选框(CheckBox)的使用

    1.RadioButton (1)介绍 (2)单选按钮点击事件的用法 (3)RadioButton与RadioGroup配合使用实现单选题功能 (4)xml布局及使用 <?xml version ...

  3. Android控件-单选按钮RadioButton

    RadioGroup单选按钮用法,还是先看效果图 先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择.下面上代码,main.xml: <RadioGroup an ...

  4. wpf 自定义单选按钮 RadioButton

    新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...

  5. Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用

    1.相关简介 RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式! Checkbox是单独使用,本文为了方便放在了RadioGroup中! 2.简单使用 方法 ...

  6. 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法

    单选按钮(RadioButton)和复选框(CheckBox).状态开关按钮(ToggleButton)与开关(Switch)是用户界面中最普通的UI组件,他们都继承了Button类,因此都可直接使用 ...

  7. CheckBox和RadioButton

    多选按钮CheckBox的使用方法和常用的监听器:OnClickListener.OnCheckedChangeListener 在activity_main.xml中使用LinearLayout布局 ...

  8. Android的RadioButton和checkBox的用法-android学习之旅(十九)

    RadioButton和checkBox简介 单选按钮(RadioButton)和复选框(CheckBox)都继承了Button,因此属性的设置和Button差不多,只是加了一个android:che ...

  9. Spring MVC单选按钮

    以下示例显示如何在使用Spring Web MVC框架的表单中使用单选按钮(RadioButton).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Fra ...

  10. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

随机推荐

  1. Vue 不睡觉教程3 - 来点实在的:自动计算剩余时间的任务列表

    目标前两课教的是入门和文件结构.都没有什么实在的东西.这次我们要来点实在的.我们要做出一个待办列表.这个待办列表有以下特点: 可以自动从文本中抽取出这件事情的开始时间可以显示当前距离这件事情的开始时间 ...

  2. ui2-3

    2016.9讲义 一.课程的主要内容和目的 二.课程所用工具软件——Photoshop CS6 1. Photoshop 的发展史 1990.2,ps1.0问世,1991.2,PS2.0发行,此后,进 ...

  3. 关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题

    关于maven打包fat jar (uber jar) 时需要包含本地文件系统第三方jar文件的问题,今天折腾了一整天.最后还是用了spring boot来做.下面是几篇关于打包的有参考价值的文章,以 ...

  4. SqlServer子查询、高级

    子查询:把一个结果集让别人继续分析查询的就叫子查询 子查询如果定义了别名,在查询引用时,必须使用别名 --子查询定义了别名,引用就必须用别名 select id,n from Person,(sele ...

  5. redux进阶 --- 中间件和异步操作

    你为什么需要异步操作? https://stackoverflow.com/questions/34570758/why-do-we-need-middleware-for-async-flow-in ...

  6. JavaScript数据结构-8.双向链表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. unity字库精简

    有2种办法,具体看情况使用 1.unity自带功能 选择放入的字体,修改Character项为"Custom set",接着出现Custom Chars中输入你想使用的字符串,字符 ...

  8. vue数组进行分组

    数组进行分组使用switch方法 <template> <v-layout> <v-card contextual-style="dark" v-if ...

  9. (转载)GRASP职责分配原则

    GRASP (职责分配原则) 要学习设计模式,有些基础知识是我们必须要先知道的,设计模式是关于类和对象的一种高效.灵活的使用方式,也就是说,必须先有类和对象,才能有设计模式的用武之地,否则一切都是空谈 ...

  10. 原来你是这样的http2......

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由mariolu发表于云+社区专栏 序言 目前HTTP/2.0(简称h2)已经在广泛使用(截止2018年8月根据Alexa流行度排名的头 ...