天天看點

android學習--DatePickerDialog和TimePickerDialog的使用

android學習--DatePickerDialog和TimePickerDialog的使用
android學習--DatePickerDialog和TimePickerDialog的使用

以DatePickerDialog為例吧,TimePickerDialog的使用大同小異。

1、使用匿名内部類聲明一個監聽器,監聽DatePickerDialog的确認按鈕。

DatePickerDialog.OnDateSetListener onDateSetListener = new OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

//Java中monthOfYear in (0:11),是以實際月份為(monthOfYear + 1)

tvShowDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);

}

};

2、複寫OnCreateDialog(int id)方法

@Override

protected Dialog onCreateDialog(int id){

Calendar calendar = Calendar.getInstance();

switch(id){

case DATE_PICKER_ID:

return new DatePickerDialog(this, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

}

return null;

}

3、在需要的時候調用showDialog方法

showDialog(DATE_PICKER_ID);

主要的就這些代碼,就可以實作上第一幅圖--日期設定。

下面給出完整代碼(包括時間設定)

MainActivity.java

import java.util.Calendar;
import com.example.datetime.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {

	private Button btnShowDatePicker = null;
	private Button btnShowTimePicker = null;
	
	//用于顯示設定的日期
	private TextView tvShowDate = null;	
	//用于顯示設定的時間
	private TextView tvShowTime = null;

	// 該常量用來辨別DatePickerDialog
	private static final int DATE_PICKER_ID = 1;
	//該常量辨別TimePickerDialog
	private static final int TIME_PICKER_ID = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		btnShowDatePicker = (Button) findViewById(R.id.btnShowDatePicker);
		btnShowTimePicker = (Button) findViewById(R.id.btnShowTimePicker);
		
		tvShowDate = (TextView) findViewById(R.id.showDate);
		tvShowTime = (TextView) findViewById(R.id.showTime);
		
		btnShowDatePicker.setOnClickListener(new ButtonListener());
		btnShowTimePicker.setOnClickListener(new OnClickListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				showDialog(TIME_PICKER_ID);
			}
		});
	}

	private class ButtonListener implements OnClickListener {

		@SuppressWarnings("deprecation")
		@Override
		public void onClick(View v) {
			// 用于顯示DatePickerDialog
			showDialog(DATE_PICKER_ID);
		}
	}

	// 監聽器,用于監聽使用者按下DatePickerDialog的set按鈕時,所設定的年月日資訊
	DatePickerDialog.OnDateSetListener onDateSetListener = new OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			//Java中monthOfYear in (0:11),是以實際月份為(monthOfYear + 1)
			tvShowDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
		}
	};
	
	//監聽TimePickerDialog
	TimePickerDialog.OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			tvShowTime.setText(hourOfDay+":"+minute);
		}
	};
	
	@Override
	protected Dialog onCreateDialog(int id){
		Calendar calendar = Calendar.getInstance();
		switch(id){
		case DATE_PICKER_ID:
			return new DatePickerDialog(this, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		case TIME_PICKER_ID:
			return new TimePickerDialog(this, onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
		}
		return null;		
	}
}
           

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button android:id="@+id/btnShowDatePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/showDatePickerDialog"/>
    <Button android:id="@+id/btnShowTimePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/showTimePickerDialog"/>
    
    <LinearLayout android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:orientation="horizontal"
	    android:weightSum="1.0">
        <TextView android:id="@+id/showDate"
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:gravity="right"
	        android:layout_weight=".50"
	        android:paddingRight="2dp"/>
        <TextView android:id="@+id/showTime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_weight=".50"
            android:paddingLeft="2dp"/>
    </LinearLayout>    

</LinearLayout>
           

strings.xml

<resources>
    <string name="app_name">Date&Time</string>
    <string name="showDatePickerDialog">showDatePickerDialog</string>
    <string name="showTimePickerDialog">showTimePickerDialog</string>
</resources>