一: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. [温故]图解java多线程设计模式(一)

    去年看完的<图解java多线程设计模式>,可惜当时没做笔记,导致后来忘了许多东西,打算再温习下这本书,顺便在这里记录一下~  1.顺序执行.并行.并发 顺序执行:多个操作按照顺序依次执行. ...

  2. LTE:EPC

    User Identifiers - IMSI and GUTI IMSI   A globle id that unique identifies a subscribe.It composed t ...

  3. Mac 10.12安装窗口快速缩放到一定比例的工具Moom

    说明:比如想要窗口只占用屏幕的一半时,我们的做法就是手动调节,而这款软件已经有快速按钮点一下即可. 下载: (链接: https://pan.baidu.com/s/1gfguJth 密码: wqjb ...

  4. MongoDB与c#(二)简单例子 使用1.7版本驱动

    //创建数据库链接 在1.7的版本驱动中这样写是会报 MongoServer方法已过时的            //MongoServer server =  MongoDB.Driver.Mongo ...

  5. 数据库其他注入思路 - 万能密码 - cookie注入 -搜索型注入

    另类登录注入形式: 经常有一类验证(ASP,PHP,JSP均存在),先判断user是否存在,ASP为例子:"select password from admin where user_nam ...

  6. Android版本信息及与Linux和Java的关系

    1.Android与Linux和Java的关系 Android严格来说,不能算是Linux,Android是一个统称,具体来说,是Google用了Linux的一个核心,用这个核心来管理进程,控制硬件. ...

  7. split使用和特殊使用(包括截取第一个字符后的数据)

    javaScript中关于split()的使用 1.一般使用对一个字符串使用split(),返回一个数组 例子:   var testArr = "1,2,3,4,5": var ...

  8. 剑指offer(31-35)编程题

    整数中1出现的次数(从1到n整数中1出现的次数) 把数组排成最小的数 丑数 第一个只出现一次的字符位置 数组中的逆序 31.求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数 ...

  9. js中json对象数组按对象属性排序

    在实际工作经常会出现这样一个问题:后台返回一个数组中有i个json数据,需要我们根据json中某一项进行数组的排序. 例如返回的数据结构大概是这样: { result:[ {id:,name:'中国银 ...

  10. Common class for judge IPV6 or IPV4

    import java.util.regex.Pattern; import org.apache.http.annotation.Immutable; /** * A collection of u ...