一: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. [原创]PHP使用Redis实现Session共享

    目录 前言 设计方案 1. 通过php自身session配置实现 2. 设置用户自定义会话存储函数 前言 小型web服务, session数据基本是保存在本地(更多是本地磁盘文件), 但是当部署多台服 ...

  2. Django分页类的封装

    Django分页类的封装 Django ORM  封装 之前有提到(Django分页的实现)会多次用到分页,将分页功能封装起来能极大提高效率. 其实不是很难,就是将之前实现的代码全都放到类中,将需要用 ...

  3. 解决Visual C++ Redistributable for Visual Studio 2015的安装问题(摘录)

    1. Visual C++ Redistributable for Visual Studio 2015系统要求:Windows 7情况下必须是Windows 7 with SP1.或者Windows ...

  4. Git fetch & pull 区别

    1 简单概括 2 git fetch 的用法 3 git pull的用法 文章来源:https://blog.csdn.net/qq_36113598/article/details/78906882

  5. 在进行make之前,configure的时候,请先清理config.cache

    在进行make之前,configure的时候,请先清理config.cache

  6. Windows下编程--模拟时钟的实现

    windows下编程--模拟时钟的实现: 主要可以分为几个步骤: (1)   编写按键事件处理(启动和停止时钟) (2)   编写时钟事件处理,调用显示时钟函数 (3)   编写显示时钟函数,要调用显 ...

  7. CentOS下安装Redis(转载)

    Redis是一个高性能的,开源key-value型数据库.是构建高性能,可扩展的Web应用的完美解决方案,可以内存存储亦可持久化存储.因为要使用跨进程,跨服务级别的数据缓存,在对比多个方案后,决定使用 ...

  8. shell通过ping检测整个网段IP的网络状态脚本

    要实现Ping一个网段的所有IP,并检测网络连接状态是否正常,很多方法都可以实现,下面简单介绍两种,如下:脚本1#!/bin/sh# Ping网段所有IP# 2012/02/05ip=1 #通过修改初 ...

  9. centOS 7镜像文件下载

  10. Vue 多路由文件的合并

    Vue 多路由文件的合并 1.使用的是ES6 数组的合并方法 let routes = new Set([...routes1, ...homerouters]);2.两个路由文件,导出的实际上就是一 ...