RadioGroup、RadioButton、CheckBox、Toast用法
xml布局文件如下:
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number2"
android:orientation="vertical">
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sex"
android:text="游泳"/>
<CheckBox
android:id="@+id/football"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/swim"
android:text="足球"/>
MainActivity.java的OnCreate方法中相应的代码如下:
genderGroup = (RadioGroup)findViewById(R.id.sex);
femaleButton = (RadioButton)findViewById(R.id.female);
maleButton = (RadioButton)findViewById(R.id.male);
genderGroup.setOnCheckedChangeListener(new GenderGroupListener());
swimBox = (CheckBox)findViewById(R.id.swim);
footBallBox = (CheckBox)findViewById(R.id.football);
swimBox.setOnCheckedChangeListener(new HobbykBoxListener());
footBallBox.setOnCheckedChangeListener(new HobbykBoxListener());
定义genderGroup、CheckBox的监听器,注意二者的监听器的参数不同:
class GenderGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//group点击的组的对象,checkedId组中的RadioButton对象的ID
if(femaleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
}
}
}
class HobbykBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// TODO Auto-generated method stub
//isChecked是否选中,如果选中则传入真,否则传入假
if(isChecked){
Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
RadioGroup、RadioButton、CheckBox、Toast用法的更多相关文章
- android基本控件学习-----RadioButton&CheckBox
RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- android单选按钮选择,RadioGroup,radioButton
android单选按钮选择,RadioGroup,radioButton 14. 四 / android基础 / 没有评论 单选布局绑定 如何识别选择
- Android RadioGroup/RadioButton
RadioGroup RadioButton的集合,提供多选一的机制 属性: android:orientation="horizontal/vertical&quo ...
- 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...
- JSTL select和checkbox的用法
select的 用法 <select id="roleIds" name="roleIds" multiple="true" back ...
- [Android]--RadioGroup+RadioButton实现底部导航栏
RadioGroup+RadioButton组合方式打造简单实用的底部导航栏 代码块: <?xml version="1.0" encoding="utf-8&qu ...
- [安卓] 4、CheckBox、RadioButton和Toast简单用法
和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChange ...
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...
随机推荐
- WPF整理-使用用户选择主题的颜色和字体
“Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows ...
- Java的书写格式,标识符及命名规则,注释
Java的书写格式,标识符及命名规则,注释 1.Java语言的书写格式(约定成俗) 1) 大括号要对齐(左大括号与句尾对其,后面大括号与句头对齐),并且成对写 2) 左大括号前面有空格 3) 遇到左大 ...
- XMLHTTPRequest对象的创建与浏览器的兼容问题
MLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHttpRequest 对象开始. 了解XMLHttpRequest 对象就先从创建XMLHttpRequest ...
- (一)Linux相关内容的简介
1.Linux内核官网:www.kernel.org 2.Linux主要发行版本: (1)redhat 服务器中常见(部分功能收费); (2)Ubuntu 图形界面较好,但是服务器中很少用: (3 ...
- RSA密钥生成与使用
RSA密钥生成与使用 openssl生成工具链接:http://pan.baidu.com/s/1c0v3UxE 密码:uv48 1. 打开openssl密钥生成软件打开 openssl 文件夹下的 ...
- iOS 开发快速导引:TableView 和 CoreData【草】
所有列表式的数据都是用 TableView 显示的 预览 待补充 原料 NSFetchedResultsController 用来操作 NSFetchRequst,有执行查询,监听变化,数据缓存等功能 ...
- .NET程序反汇编JustDecompile 开源
JustDecompile是Telerik公司推出一个免费的.net反编译工具,支持插件,与Visual Studio 集成,能够创建Visual Studio project文件.JustDecom ...
- 关于EF6的记录Sql语句 与 EntityFramework.Extend 的诟病
1.关于EF6的记录Sql语句,一个老生长谈的问题. 他生成的sql语句实在是烂,大家都这样说 2.EF6 更新删除不方便,没有批量操作.所以,有人出了EF6.Extend 大家用起来也很爽 基于以 ...
- 让你的站点也支持MarkDown
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过类似HTML的标记语法,它可以使普通文本内容具有一定的格式.Markdown的语法简洁明了.学习容易,而且功能比纯文本更强,因此有很多人 ...
- ASP.NET MVC 控制器激活(三)
ASP.NET MVC 控制器激活(三) 前言 在上个篇幅中说到从控制器工厂的GetControllerInstance()方法来执行控制器的注入,本篇要讲是在GetControllerInstanc ...