一步一步学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的使用,如果你也已经写好了列表那么恭喜这一 ...
随机推荐
- iOS进阶面试题----经典10道
OneV‘s Den在博客里出了10道iOS面试题,用他的话是:"列出了十个应聘Leader级别的高级Cocoa/CocoaTouch开发工程师所应该掌握和理解的技术" . 在这 ...
- js模板引擎--artTemplate
js模板引擎--artTemplate 以前研究过一段时间的handlebars,但因为其渲染性能略逊于腾讯的artTemplate(在artTemplate的GitHub官网上有推荐的性能测试地址) ...
- Qt控件精讲一:按钮
原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...
- 基于visual Studio2013解决面试题之0702输出数字
题目
- awakeFromNib小总结
awakeFromNib 在使用IB的时候才会涉及到此方法的使用,当.nib文件被载入的时候,会发送一个awakeFromNib的消息到.nib文件里的每一个对象,每一个对象都能够定义自己的awake ...
- UVA 10160 Servicing Stations(深搜 + 剪枝)
Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...
- POJ2392 SpaceElevator [DP]
题目大意:有一头奶牛要上太空,他有非常多种石头,每种石头的高度是hi,可是不能放到ai之上的高度.而且这样的石头有ci个 将这些石头叠加起来.问可以达到的最高高度. 解题思路:首先对数据进行升序排序. ...
- oradebug推进scn
有时候我们遇到例如以下错误: ORA-01092: ORACLE instance terminated. Disconnection forced ORA-00600: internal error ...
- UltraEdit配置python和lua环境
[语法高亮] 在UltraEdit的wordfile中添加python和lua的语法支持(红色的为python,蓝色的为lua): /L10"Python" Line Commen ...
- CMDeviceMotion使用
CMDeviceMotion使用 by 吴雪莹 manager = [[CMMotionManager alloc] init]; ViewController *__weak weakSelf=se ...