在平常使用软件的时候,我们经常会碰见一些选择题,例如选择性别的时候,在男和女之间选,前面说过这个情况要用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. AngelHack China 2013 招组队成员

    AngelHack China 2013 connect me

  2. jquery如何在加载完iframe的内容后才进行下一步操作

    为iframe添加onload事件 ie使用attachEvent("onload",function(){}) firefox.chrome使用addEventListener( ...

  3. PO经批准的订单API

    DECLARE l_return_status VARCHAR2(1); l_exception_msg VARCHAR2(4000); BEGIN mo_global.set_policy_cont ...

  4. Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)

    1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...

  5. 大豆生物柴油驱动的大巴斯(Bus)

    请看下图: 这是大豆生物柴油(Soybean biodiesel)驱动的大巴斯(Bus)的外观,感觉非常有劲. 回想历史,1893年,德国学者Rudolf Diesel(1858-1913)发明了一种 ...

  6. org.apache.hadoop.ipc.Client: Retrying connect to server异常的解决

    检查发现是DataNode一直连接不到NameNode. 检查各个节点在etc/hosts中的配置是否有127.0.1.1 xxxxxx.如果有把其屏蔽或者删除,重启各节点即可. 原因:127.0.1 ...

  7. 一个上传EXCEL导入示例

    REPORT ZTEST_UPEXCEL. data: gt_table type TABLE OF SFLIGHT, gs_table like line of gt_table. DATA:lt_ ...

  8. 14.1.2 Checking InnoDB Availability 检查InnoDB 可用性:

    14.1.2 Checking InnoDB Availability 检查InnoDB 可用性: 确认你的server 是否支持InnoDB,使用 SHOW ENGINES 语句.(默认MySQL的 ...

  9. 基于visual Studio2013解决C语言竞赛题之1094纵横图

        题目 解决代码及点评 /************************************************************************/ /* ...

  10. Swift - 导航条(UINavigationBar)的使用

    与导航控制器(UINavigationController)同时实现导航条和页面切换功能不同. 导航条(UINavgationBar)可以单独使用,添加至任何的UIView中.UINavigation ...