码迷,mamicode.com
首页 > 移动开发 > 详细

Android--OnCheckedChangeListener实例

时间:2015-01-15 13:03:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:android   oncheckedchangeliste   

.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

Android--OnCheckedChangeListener实例

标签:android   oncheckedchangeliste   

原文地址:http://blog.csdn.net/yayun0516/article/details/42738057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!