从零开始学android:Android事件处理—单选钮与OnCheckedChangeListener
来源:互联网 发布:类似记事本的软件 编辑:程序博客网 时间:2024/05/16 05:42
OnCheckedChangeListener
单选钮(RadioGroup)上也可以进行事件的处理操作,当用户选中了某选项之后也将触发相应的监听器进行若干处理,而注册事件的方法为:
public void setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)。
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/show" android:text="您的姓名是:" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content" 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></LinearLayout>
程序文件:
package com.richard.onclickedchangelistener;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class MainActivity extends Activity {private TextView show = null;private RadioGroup sex = null;private RadioButton male = null;private RadioButton female = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.show = (TextView) super.findViewById(R.id.show);this.sex = (RadioGroup) super.findViewById(R.id.sex);this.male = (RadioButton) super.findViewById(R.id.male);this.female = (RadioButton) super.findViewById(R.id.female);this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());}private class OnCheckedChangeListenerImpl implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {String temp = null;if(MainActivity.this.male.getId() == checkedId){temp = MainActivity.this.male.getText().toString();//取得单选文本}if(MainActivity.this.female.getId() == checkedId){temp = MainActivity.this.female.getText().toString();//取得单选文本}MainActivity.this.show.setText("您的性别是:"+temp);//设置文本信息}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
测试效果:
- 从零开始学android:Android事件处理—单选钮与OnCheckedChangeListener
- 第20章、OnCheckedChangeListener事件(从零开始学Android)
- 第20章、OnCheckedChangeListener事件(从零开始学Android)
- 第20章、OnCheckedChangeListener事件(从零开始学Android)
- 从零开始学android:Android事件处理—单击事件
- 从零开始学android:Android事件处理—焦点事件
- 从零开始学android:Android事件处理—长按事件
- 从零开始学android:Android事件处理—键盘事件
- 从零开始学android:Android事件处理—触摸事件
- 从零开始学android:Android事件处理—下拉列表框与OnItemSelectedListener
- 从零开始学android:Android事件处理—监听日期与时间的改变
- 从零开始学android<android事件的处理方式.二十四.>
- 第18章、基于监听器的事件处理(从零开始学Android)
- 第18章、基于监听器的事件处理(从零开始学Android)
- 第18章、基于监听器的事件处理(从零开始学Android)
- 《Android开发从零开始》——20.广播事件处理(1)
- 《Android开发从零开始》——21.广播事件处理(2)
- Android--OnCheckedChangeListener实例
- 深入分析qsort库函数
- 矩阵最大值
- 把 对象存到 文件中
- 用JSP创建一个表格模板
- 使用Java Annotation写Excel 工具类
- 从零开始学android:Android事件处理—单选钮与OnCheckedChangeListener
- wikioi 1000 A+B
- directshow的下载、VC2008中的配置、入门教程以及如何在opencv中使用
- java学习笔记1——面向对象的一些性质
- Gcc 编译器的常识
- linux /proc/pid/maps 信息分析
- 程序员的自我修养
- 编程乐趣:C#实现12306自动登录(2013年11月27)
- 文件服务器 FTP服务器