在平常使用软件的时候,我们经常会碰见一些选择题,例如选择性别的时候,在男和女之间选,前面说过这个情况要用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之事件篇——单选按钮监听事件的更多相关文章

  1. Android 自定义ScrollView的滑动监听事件

    项目结构: 1.LazyScrollView类(自定义ScrollView) package android.zhh.com.myapplicationscrollview; /** * Create ...

  2. Vue自定义事件,$on(eventName) 监听事件,$emit(eventName) 触发事件

    <!--自定义事件 使用 $on(eventName) 监听事件 使用 $emit(eventName) 触发事件--> <div id="app15"> ...

  3. Android开发入门——Button绑定监听事件三种方式

    import android.app.Activity; import android.os.Bundle;import android.view.View;import android.widget ...

  4. Android中Button的五种监听事件

    简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...

  5. Android开发 ---基本UI组件8:九宫格布局、setOnItemClickListener()项被选中监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  6. Android——监听事件总结

    各种监听事件 1.按钮 Button(1)点击监听 btn_1.setOnClickListener(new View.OnClickListener() { (2)长按监听 btn_1.setOnL ...

  7. 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 ...

  8. Android CheckBox的监听事件

    1.在xml文件中定义CheckBox,一定要定义id <CheckBox android:id="@+id/beijing" android:layout_width=&q ...

  9. android listview 的监听事件

    今天遇到了一个比较让我头疼的问题,不过追根揭底只是我对listview理解的不够透彻罢了, 闲言少叙,说说我遇到的问题吧: 上篇随笔我写了关于listview的使用,如果你也已经写好了列表那么恭喜这一 ...

随机推荐

  1. hdu4709求三角形面积

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  2. MFC模板CArray及其派生类

    CArray及其派生类 1. 简介:访问方法及效率和普通的数组一样,比普通数组强大的功能是可以改变数组的大小.Array采用队列方式存储数据,因而其内部数据元素是以物理方式顺序排列的,所以检索.顺序执 ...

  3. POJ2392 SpaceElevator [DP]

    题目大意:有一头奶牛要上太空,他有非常多种石头,每种石头的高度是hi,可是不能放到ai之上的高度.而且这样的石头有ci个 将这些石头叠加起来.问可以达到的最高高度. 解题思路:首先对数据进行升序排序. ...

  4. 关于CopyU!的常见问题解答

    拷优(CopyU!)常见问题解答 本常见问题解答列举了一些常见的疑问及其解释,如果您对CopyU!有任何问题,请您首先查看本解答! 本解答将会保持随时更新! 一.使用篇:     1.问:我的杀毒软件 ...

  5. Ace of Aces

    Description There is a mysterious organization called Time-Space Administrative Bureau (TSAB) in the ...

  6. Git Hub,eclipse pull 出现问题

    一般在eclise里面使用geithub,之后会出现无法pull,或者pull 报错的问题.这里需要修改本地库的配置文件 The current branch is not configured fo ...

  7. [置顶] location.href你真的会用了?

    *.location.href 用法: top.location.href=”url”          在顶层页面打开url(跳出框架) self.location.href=”url”       ...

  8. mysql 创建函数

    <pre name="code" class="html">root 用户创建函数: delimiter $$ CREATE FUNCTION `l ...

  9. 工信部表态支持Linux,可是Linux又是什么呢?

    近日,工信部高层官员出面表态:工信部大力支持发展国产Linux操作系统,可是,Linux又是什么呢?假设依照工信部的说法,发展所谓"国产Linux".恐怕要给国家带来麻烦. 大家知 ...

  10. 屏幕编程 F4的帮组用法

    PROCESS ON VALUE-REQUEST. * 设置帮助(工作中心)  FIELD wa_zppt026-arbpl MODULE mdl_arbpl_f4. *&---------- ...