Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android筆記——date&time(日期時間選擇對話框)

Android筆記——date&time(日期時間選擇對話框)

編輯:關於Android編程

TimePickerDialog(時間選擇對話框)

創建TimePickerDialog時間選擇對話框:

1.創建一個類繼承DialogFragement

2.重寫onCreateDialog()方法,返回一個TimePickerDialog對象

3.實現TimePickerDialog的OnTimeSetListener接口來接收一個回調,當用戶設置時間

DatePickerDialog(日期選擇對話框)

創建DatePickerDialog日期選擇對話框:

1.創建一個類繼承DialogFragment

2.重寫onCreateDialog()方法,返回一個TimePickerDialog對象

3.實現DatePickerDialog的OnDateSetListener接口來接收一個回調,當用戶設置日期


DatePickerDialog和TimePickerDialog是Android提供的可以彈出一個日期、時間選擇的對話框,我們可以在程序中通過實例化DatePickerDialog和TimePickerDialog來得到一個日期、時間對話框。這兩個類都是AlertDialog的子類

我提供了一個百度的關於Picker的官方說明,可能對理解這篇文章有幫助:http://wenku.baidu.com/link?url=U77TPsl99lpf8QrGD2h6XKA6xJqzk-JseCnJhC9VmgYoWckhCZ8-3F3TzOkOZGLverjwt6JitlhW-wWMODAOQLtEpVxryaZxNlCQstOjLCK

點擊打開鏈接

源碼:

html



    


java

import java.util.Calendar;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

@SuppressLint({ "NewApi", "ValidFragment" })
public class MainActivity extends Activity {
	private Button btnSetTime;
	private TextView tv_setTime;
	private TextView tv_setDate;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
               //實例化對象,通過findViewById和相應的xml布局組件綁定
                btnSetTime = (Button) findViewById(R.id.button1_settime);
		tv_setTime = (TextView) findViewById(R.id.tv_settime);
		tv_setDate = (TextView) findViewById(R.id.tv_setdate);
	}

	//設置日期事件處理方法
	public void setDateClick(View v){
		SetDateDialog sdt = new SetDateDialog();
		sdt.show(getFragmentManager(), "datePicker");
	}
	
	//設置時間事件處理方法
	public void setTimeClick(View v){
		SetTimeDialog std = new SetTimeDialog();
		std.show(getFragmentManager(), "timePicker");
	}


	public 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;
	}
	
	//創建日期選擇對話框
	class SetDateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{

		public Dialog onCreateDialog(Bundle savedInstanceState) {
		        //Calendar 是一個抽象類,是通過getInstance()來獲得實例,設置成系統默認時間
			 final Calendar c = Calendar.getInstance();
			//獲取年,月,日
                        int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH);
			int day= c.get(Calendar.DAY_OF_MONTH);
                  
                        DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);
			
			return dpd;
		}
		public void onDateSet(DatePicker view, int year, int month, int day) {
			tv_setDate.setText(year+"年"+(month+1)+"月"+day+"日");
		}
		
	}
	//創建時間選擇對話框
	@SuppressLint("NewApi")
	 class SetTimeDialog extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
		
		@SuppressLint("NewApi")
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			//獲取小時,分鐘
                        final Calendar c = Calendar.getInstance();
			int hour = c.get(Calendar.HOUR_OF_DAY);
			int minute = c.get(Calendar.MINUTE);
			
                        TimePickerDialog tpd = new TimePickerDialog(getActivity(),this,hour, minute, DateFormat.is24HourFormat(getActivity()));
			return tpd;
		}

		public void onTimeSet(TimePicker view, int hourOfDay, int minute){
			
			tv_setTime.setText(hourOfDay+":"+minute);
		}
	}
}

效果展示:

\ \

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved