一步一步学android之事件篇——单选按钮监听事件
在平常使用软件的时候,我们经常会碰见一些选择题,例如选择性别的时候,在男和女之间选,前面说过这个情况要用RadioGroup组件,那么点击了之后我们该怎么获取到选择的那个值呢,这就是今天要说的OnCheckedChangeListener方法。这个方法定义如下:
public static interface RadioGroup.OnCheckedChangeListener{
public void onCheckedChanged(RadioGroup group, int checkedId){
}
}
下面写个例子来看看如何监听单选按钮,效果如下:
点击前:
点击后:
下面给出程序源文件:
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="请选择您的性别...."/>
<RadioGroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/male">
<RadioButton
android:id="@+id/male"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
<TextView
android:id="@+id/showSex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的性别是:"/>
</LinearLayout>
MainActivity.java:
package com.example.oncheckedchangedlistenerdemo; import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView showSex = null;
private RadioGroup sex = null;
private RadioButton male = null;
private RadioButton female = null;
private String head = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView(){
//初始化组件
showSex = (TextView)super.findViewById(R.id.showSex);
sex = (RadioGroup)super.findViewById(R.id.sex);
male = (RadioButton)super.findViewById(R.id.male);
female = (RadioButton)super.findViewById(R.id.female);
//获取显示前缀(即您的性别是:),以便显示美观
head = showSex.getText().toString();
//未调用监听时显示默认选择内容
if(male.getId() == sex.getCheckedRadioButtonId()){
showSex.setText(head+male.getText().toString());
}else if(female.getId() == sex.getCheckedRadioButtonId()){
showSex.setText(head+female.getText().toString());
}
//为RadioGroup设置监听事件
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
String sexTmp = "";
if(checkedId == male.getId()){
sexTmp = male.getText().toString();
}else if(checkedId == female.getId()){
sexTmp = female.getText().toString();
}
showSex.setText(head+sexTmp);
}
});
}
}
今天就到这里了。
一步一步学android之事件篇——单选按钮监听事件的更多相关文章
- Android 自定义ScrollView的滑动监听事件
项目结构: 1.LazyScrollView类(自定义ScrollView) package android.zhh.com.myapplicationscrollview; /** * Create ...
- Vue自定义事件,$on(eventName) 监听事件,$emit(eventName) 触发事件
<!--自定义事件 使用 $on(eventName) 监听事件 使用 $emit(eventName) 触发事件--> <div id="app15"> ...
- Android开发入门——Button绑定监听事件三种方式
import android.app.Activity; import android.os.Bundle;import android.view.View;import android.widget ...
- Android中Button的五种监听事件
简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...
- Android开发 ---基本UI组件8:九宫格布局、setOnItemClickListener()项被选中监听事件
效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...
- Android——监听事件总结
各种监听事件 1.按钮 Button(1)点击监听 btn_1.setOnClickListener(new View.OnClickListener() { (2)长按监听 btn_1.setOnL ...
- Android 属性动画监听事件与一个菜单的例子
简单监听事件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...
- Android CheckBox的监听事件
1.在xml文件中定义CheckBox,一定要定义id <CheckBox android:id="@+id/beijing" android:layout_width=&q ...
- android listview 的监听事件
今天遇到了一个比较让我头疼的问题,不过追根揭底只是我对listview理解的不够透彻罢了, 闲言少叙,说说我遇到的问题吧: 上篇随笔我写了关于listview的使用,如果你也已经写好了列表那么恭喜这一 ...
随机推荐
- 浅析js的执行顺序
javascript是一种描述型的脚本语言,是一种解析语言,由浏览器动态解析,不同种类的浏览器不同版本的浏览器对于js的解析有着微小的差别,不同浏览器的js解析引擎效率也有高低,下面来给大家分析一下j ...
- .Net有许多Office,PDF,Email,HTML的控件
比如: Aspose.Total for .NET includes the following components: Aspose.Words for .NET 16.3.0 (4/13/2016 ...
- MFC获取rgb图像数据后动态显示及保存图片的方法
该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw32.lib 链接库 //---------------- ...
- 基于visual Studio2013解决C语言竞赛题之1085相邻之和素数
题目 解决代码及点评 /************************************************************************/ /* ...
- HDU - 4944 FSF’s game
Problem Description FSF has programmed a game. In this game, players need to divide a rectangle into ...
- Android学习路线(二十四)ActionBar Fragment运用最佳实践
转载请注明出处:http://blog.csdn.net/sweetvvck/article/details/38645297 通过前面的几篇博客.大家看到了Google是怎样解释action bar ...
- 汉字转拼音再转ASCII
汉字能够转成拼音.能够在转成ASCII码,然后就能够转成十六进制数,再就能够转成0和1组成的二进制帧了! 比方说: 我爱你 -> wo ai ni -> 119 111 32 97 105 ...
- keil uVision4的安装以及KEIL_Lic.exe的注冊
1.首先毋庸置疑,在网上下载keil uVision4的EXE可运行文件,可能存在两个版本号.51核的单片机(33.3M)和微控制器开发合集(244M),可依据自己的实际须要选择.没有必要都装 2.依 ...
- Delphi/C#之父首次访华:55岁了 每天都写代码
Delphi.C#之父Anders Hejlsberg 近日首次访华,并在10月24日和27日参加了两场见面会,分享了他目前领导开发的TypeScript项目,并与国内前端开发者近距离交流.本文就为读 ...
- VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用(简单明了)
一. 在字符串前加一个L作用: 如 L"我的字符串" 表示将ANSI字符串转换成unicode的字符串,就是每个字符占用两个字节. strlen("as ...