Android控件:RadioButton(单选button)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="400" height="600" alt="">
首先,在布局文件 activity_main.xml中注冊一个RadioGroup,并为RadioGroup设置监听,图中两个RadioButton为一个RadioGroup。
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"
tools:context=".MainActivity"> <TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/radioGroupID"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <RadioButton
android:id="@+id/femaleGroupID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="female"
/>
<RadioButton
android:id="@+id/maleGroupID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="male"/>
</RadioGroup> </LinearLayout>
MainActivity.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup; public class MainActivity extends ActionBarActivity { private RadioGroup radioGroup;
private RadioButton femaleRadioButton;
private RadioButton maleRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//载入布局文件
setContentView(R.layout.activity_main);
//获取实例
radioGroup=(RadioGroup)findViewById(R.id.radioGroupID);
femaleRadioButton=(RadioButton)findViewById(R.id.femaleGroupID);
maleRadioButton=(RadioButton)findViewById(R.id.maleGroupID);
//设置监听
radioGroup.setOnCheckedChangeListener(new RadioGroupListener());
}
//定义一个RadioGroup的OnCheckedChangeListener
//RadioGroup 绑定的是RadioGroup.OnCheckedChangeListener
//CheckBox 绑定的是CompoundButton.OnCheckedChangeListener 或者 view.OnClickListener class RadioGroupListener implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId==femaleRadioButton.getId()){
System.out.println("选中了female!");
}else if (checkedId==maleRadioButton.getId()){
System.out.println("选中了male!");
}
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
日志显演示样例如以下:
Android控件:RadioButton(单选button)的更多相关文章
- android 控件: xml 设置 Button 按下背景
本篇文章讲述了不使用java代码来改变 Button 按下和未按下时的背景. 首先准备两张图片, 分别是按钮按下和按钮未按下的. 在res/drawable 文件夹中创建一个button_select ...
- Android控件之圆形Button
bg_circle.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns: ...
- Android控件系列之RadioButton&RadioGroup(转)
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...
- Android控件系列之CheckBox
学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...
- Android控件之Button(按钮控件)和ImageButton(图片按钮控件)
一.Button和ImageButton特证: 1.共同特证: 都可以作为一个按钮产生点击事件 2.不同特证: Button有text的属性,ImageButton没有 ImageButton有src ...
- Android控件篇
Android中提供了丰富的UI空间.为了最大限度地发挥平台的性能.每个开发人员必须熟练掌握UI控件尤其是经常使用的UI控件.并能依据须要呈现的内容选择最恰当的控件. Android提供了XML配置和 ...
- 一步一步学android控件(之十六)—— CheckBox
根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...
- Android控件介绍
1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...
- ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
- 单选按钮控件(Ridio Button)的使用
VC学习笔记5:单选按钮控件(Ridio Button)的使用 一.对单选按钮进行分组: 每组的第一个单选按钮设置属性:Group,Tabstop,Auto;其余按钮设置属性Tabstop,Auto. ...
随机推荐
- git如何从远程非master分支更新到本地对应分支
git如何从远程非master分支更新到本地对应分支 自己实例 正确步骤 如果本地有分支,那就删除本地分支 删除本地分支::git branch -d 2018_4_18_second 切换分支: g ...
- 远程带参数POST访问接口,返回数据
1. string token = GetRequest.GetString("token"); int customer_id = GetRequest.GetInt(" ...
- 威联通212P 在admin用户密码正确情况下仍然无法登录WEB页面解决办法
*登录 telnet 执行以下语句: [~] # cp /etc/default_config/passwd /mnt/HDA_ROOT/.config/passwd[~] # cp /etc/def ...
- Debounce 和 Throttle【转载】
在处理诸如 resize.scroll.mousemove 和 keydown/keyup/keypress 等事件的时候,通常我们不希望这些事件太过频繁地触发,尤其是监听程序中涉及到大量的计算或者有 ...
- GridView单元格取值显示为
在通过GridView取一个单元格(cell)的值时,数据库中为NULL,而页面上显示为空格.发现通过gridview.cell[i].text取出来的值为 ,导致获取数据出现问题. 解决方法: 一. ...
- 紫书 例题 9-13 UVa 1220 (最大独立子集)
这里的状态定义的非常的巧妙,d(i, 1)表示以i为根节点且选i的子树的最大独立子集 d(i, 0)表示以i为根节点且不选i的子树的最大独立子集 d(i, 1) = sum{ d(v, 0) | v是 ...
- Centos6.5 安装lnmp环境
最近项目要配置在nginx上,所以搜索了下具体nginx的安装,看到这篇文章简洁明了而且测试成功就借用了,作品出处:http://www.cnblogs.com/xiaoit/p/3991037.ht ...
- 洛谷 P2309 loidc,卖卖萌
P2309 loidc,卖卖萌 题目背景 Loidc萌萌哒. 他最近一直在靠卖萌追求他的真爱——vivym,经过几轮攻势后vivym酱眼看就要被他所攻略.擅长数据结构的vivym决定利用强大的数据结构 ...
- Android自己定义视图(一):带下划线的TextView
package com.francis.underlinetextviewtest; import android.content.Context; import android.content.re ...
- js --- 事件冒泡 事件捕获
先上结论: 他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事 ...