现在的位置: 首页 > 综合 > 正文

22_单选按钮与OnCheckedChangeListener和监听日期与时间的改变

2013年03月12日 ⁄ 综合 ⁄ 共 4149字 ⁄ 字号 评论关闭

1. 单选按钮与OnCheckedChangeListener

单选按钮(RadioGroup)上也可以进行事件处理操作,当用户选中了某选项后也将会触发相应的监听器进行处理,注册事件的方法为:

publicvoid setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)

例1

程序运行效果截图:

实现过程:

(1)编写main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00ff00">
<TextView 
    android:id="@+id/show"     
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="请选择您的性别:"
    android:textSize="25px"/>
<RadioGroup 
    android:id="@+id/sex"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton 
        android:id="@+id/male"
        android:text="男"/>
    <RadioButton 
        android:id="@+id/female"
        android:text="女"/>
</RadioGroup>
</LinearLayout>

(2) 编写MainActivity.java

import android.app.Activity;
import android.os.Bundle;
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;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		show = (TextView)findViewById(R.id.show);
		sex = (RadioGroup)findViewById(R.id.sex);
		male = (RadioButton)findViewById(R.id.male);
		female = (RadioButton)findViewById(R.id.female);
		sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());
	}
	private class OnCheckedChangeListenerImpl implements OnCheckedChangeListener{
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId){
			String temp = null;
			if(male.getId() == checkedId){
				temp = male.getText().toString();
			}
			if(female.getId() == checkedId){
				temp = female.getText().toString();
			}
			show.setText("您的性别是:" + temp);
		}
	}
}

2. 监听日期与时间的改变

  日期选择器(DatePicker)与时间选择器(TimePicker)可以用于进行日期与时间的调整,当它们进行调整时也可以用相关的监听器对其状态进行监听。

  日期监听器接口:android.widget.DatePicker.OnDateChangedListener

  时间监听器接口:android.widget.TimePicker.OnTimeChangedListener

例2

程序运行效果截图

实现过程:

1. 编写布局文件man.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00ff00">
	<EditText 
	    android:id="@+id/input"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"/>
	<LinearLayout
	    android:orientation="horizontal"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent">
	    <DatePicker
	        android:id="@+id/date"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	    <TimePicker 
	        android:id="@+id/time"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
</LinearLayout>

2. 编写MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends Activity {
	private EditText input = null;
	private DatePicker date = null;
	private TimePicker time = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		input = (EditText)findViewById(R.id.input);
		date = (DatePicker)findViewById(R.id.date);
		time = (TimePicker)findViewById(R.id.time);
		time.setIs24HourView(true);
		time.setOnTimeChangedListener(new OnTimeChangedListenerImpl());
		date.init(date.getYear(), (date.getMonth() + 1), date.getDayOfMonth(),
				new OnDateChangedListenerImpl());
		setDateTime();
	}
	public void setDateTime(){
		input.setText(date.getYear() + "-" + date.getMonth() + "-" + date.getDayOfMonth()
				+ " " + time.getCurrentHour() + ":" + time.getCurrentMinute());
	}
	private class OnDateChangedListenerImpl implements OnDateChangedListener{
		public void onDateChanged(DatePicker view, int year, int monghOfYear,
				int dayOfMonth){
			setDateTime();//日期改变时调用
		}
	}
	private class OnTimeChangedListenerImpl implements OnTimeChangedListener{
		@Override
		public void onTimeChanged(TimePicker view, int hourOfDay, int minute){
			setDateTime();//时间改变时调用
		}
	}
}

抱歉!评论已关闭.