当前位置: 代码迷 >> Android >> Android-OnCheckedChangeListener范例
  详细解决方案

Android-OnCheckedChangeListener范例

热度:59   发布时间:2016-04-28 02:48:47.0
Android--OnCheckedChangeListener实例

.xml代码如下:

<?xml version="1.0" encoding="utf-8"?><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:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="您的性别是:"    android:textSize="20px"    /><RadioGroup     android:id="@+id/sex"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    android:checkedButton="@+id/male"    >    <RadioButton    android:id="@+id/male"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="男"/>    <RadioButton    android:id="@+id/female"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="女"/>    </RadioGroup></LinearLayout>

.java 代码如下:

package org.lxh.demo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class Hello extends Activity {	private TextView text=null;	private RadioGroup sex=null;	private RadioButton male=null;	private RadioButton female=null;	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState); // 生命周期方法		super.setContentView(R.layout.main); // 设置要使用的布局管理器		this.text=(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 OnCheckedChangeListerImpl());		}	private class OnCheckedChangeListerImpl implements OnCheckedChangeListener{		public void onCheckedChanged(RadioGroup group, int checkedId) {			String temp=null;			if(Hello.this.male.getId()==checkedId){				temp=Hello.this.male.getText().toString();			}			if(Hello.this.female.getId()==checkedId){				temp=Hello.this.female.getText().toString();			}			Hello.this.text.setText("您的性别是:"+temp);					}			}}

运行效果:


源码下载:http://download.csdn.net/detail/yayun0516/8363587

  相关解决方案