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. ...
随机推荐
- php实现简单算法2
php实现简单算法2 去弄php手册,里面有数据结构,有数据结构就好办了,我的算法基础那么好. 而且的话有数据结构的话再配合我脑中的那些算法了,我就都ok啦. 在手册里面搜索php数据结构就好 路径如 ...
- 14.idea右键单击没有 svn选项处理办法
转自:http://www.cnblogs.com/xiohao/p/5044213.html 问题一: IntelliJ IDEA打开带SVN信息的项目不显示SVN信息,项目右键SVN以及图标还有C ...
- AIX 系统补丁升级步骤
AIX 系统补丁升级步骤 1.升级之前建议备份 rootvg (推荐) # smit mksysb 2.检查系统版本号 # oslevel -r 3.找到补丁光盘或者下载补丁,上传到服务器 ...
- 威联通212P 在admin用户密码正确情况下仍然无法登录WEB页面解决办法
*登录 telnet 执行以下语句: [~] # cp /etc/default_config/passwd /mnt/HDA_ROOT/.config/passwd[~] # cp /etc/def ...
- 二:2.1 字符串与循环中的 while
字符串:字符串是以单引号或双引号括起来的任意文本 创建字符串: str1 = "sunck is a good man!" str3 = "sunckis a nice ...
- 紫书 习题 10-17 UVa 11105 (筛法)
类似于素数筛的思想去做,不然暴力会超时而且还要判重 #include<cstdio> #include<cstring> #include<vector> #def ...
- 关于结构体内存对齐方式的总结(#pragma pack()和alignas())
最近闲来无事,翻阅msdn,在预编译指令中,翻阅到#pragma pack这个预处理指令,这个预处理指令为结构体内存对齐指令,偶然发现还有另外的内存对齐指令aligns(C++11),__declsp ...
- 洛谷 P2913 [USACO08OCT]车轮旋转Wheel Rotation
P2913 [USACO08OCT]车轮旋转Wheel Rotation 题目描述 Farmer John has an old-time thresher (wheat harvester) tha ...
- C++虚表的原理,很好
下面这篇文章讲的很好. http://www.cnblogs.com/lihaosky/articles/1606502.html 假设我们有这样的一个类: class Base { public: ...
- 开创学习的四核时代-iTOP-4412开发板开源硬件平台
iTOP-4412开发板如今比較热门的开发板.笔者最近入了一套. 也推荐给初学ARM的朋友学习,4412开发板搭载三星Exynos四核处理器,配备1GB内存,4GB固态硬盘EMMC存储,兼具高速读取与 ...