Android開發之DatePickerDialog與TimePickerDialog的功能和用法詳解,androiddialog用法

來源:互聯網
上載者:User

Android開發之DatePickerDialog與TimePickerDialog的功能和用法詳解,androiddialog用法

DatePickerDialog與TimePickerDialog的功能比較簡單,用法也很簡單,只要下面兩步即可。

Ø 通過new關鍵字建立DatePickerDialog、TimePickerDialog執行個體,調用它們的show()方法即可將日期選擇對話方塊、時間選擇對話方塊顯示出來。

Ø 為DatePickerDialog、TimePickerDialog綁定監聽器,這樣可以保證使用者通過DatePickerDialog、TimePickerDialog設定事件是觸發監聽器,從而通過監聽器來擷取使用者佈建的事件。

TimePickerDialog(時間選擇對話方塊)概述:

一個使用TimePicker的對話方塊,為使用者提供時間選擇。

類結構:

java.lang.Object   ↳android.app.Dialog    ↳android.app.AlertDialog     ↳android.app.TimePickerDialog

構造方法:

Public Constructors

 

TimePickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)

TimePickerDialog(Context context, int theme, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)

說明:

public TimePickerDialog (Context context, TimePickerDialog.OnTimeSetListener callBack,inthourOfDay, int minute, boolean is24HourView)

參數:

參數

說明

Context

運行組件的Activity。

callBack

使用者選擇好時間後,通知應用的回呼函數。

hourOfDay

初始的小時。

Minute

初始的分鐘。

is24HourView

是否使用24小時制。

public TimePickerDialog (Context context,int teme, TimePickerDialog.OnTimeSetListener callBack,int hourOfDay, int minute, boolean is24HourView)

參數:

參數

說明

Context

運行組件的Activity。

teme

應用在時間選擇對話方塊上的主題。

callBack

使用者選擇好時間後,通知應用的回呼函數。

hourOfDay

初始的小時。

Minute

初始的分鐘。

is24HourView

是否使用24小時制。

公有方法:

Public Methods

void

onClick(DialogInterface dialog, int which)

當對話方塊上的按鈕被單擊時這個方法將被回調。

void

onRestoreInstanceState(Bundle savedInstanceState)

從前一個儲存的bundle 中恢複對話方塊的狀態。

Bundle

onSaveInstanceState()

將對話方塊的狀態儲存到bundle中.

void

onTimeChanged(TimePicker view, int hourOfDay, int minute)

當時間被關改變的時候回調該方法。

void

updateTime(int hourOfDay, int minutOfHour)

更新時間

執行個體:
public static class TimePickerFragment extends DialogFragment                            implements TimePickerDialog.OnTimeSetListener {    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {        // Use the current time as the default values for the picker        final Calendar c = Calendar.getInstance();        int hour = c.get(Calendar.HOUR_OF_DAY);        int minute = c.get(Calendar.MINUTE);        // Create a new instance of TimePickerDialog and return it        return new TimePickerDialog(getActivity(), this, hour, minute,                DateFormat.is24HourFormat(getActivity()));    }    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {        // Do something with the time chosen by the user    }}


DatePickerDialog(時間選擇對話方塊)概述:

一個帶有的DatePicker的簡單對話方塊,為使用者提供日期選擇。

類結構:
java.lang.Object   ↳android.app.Dialog    ↳android.app.AlertDialog     ↳android.app.DatePickerDialog
構造方法:

Public Constructors

DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

說明:

public DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, intdayOfMonth)

參數:

參數

說明

Context

運行組件的Activity。

callBack

使用者選擇好日期後,通知應用的回呼函數。

year

初始的年。

monthOfYear

初始的月。

dayOfMonth

初始的天。

public DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, intdayOfMonth)

參數:

參數

說明

Context

運行組件的Activity。

theme

應用在日期選擇對話方塊上的主題。

callBack

使用者選擇好日期後,通知應用的回呼函數。

year

初始的年。

monthOfYear

初始的月。

dayOfMonth

初始的天。

公有方法:

Public Methods

DatePicker

getDatePicker()

擷取日期選擇對話方塊。

void

onClick(DialogInterface dialog, int which)

當對話方塊上的按鈕被單擊時這個方法將被回調。

void

onDateChanged(DatePicker view, int year, int month, int day)

當日期改變的時候回調該方法。

void

onRestoreInstanceState(Bundle savedInstanceState)

當對話方塊上的按鈕被單擊時這個方法將被回調。

Bundle

onSaveInstanceState()

將對話方塊的狀態儲存到bundle中。

void

updateDate(int year, int monthOfYear, int dayOfMonth)

設定當前日期。

 執行個體:
public static class DatePickerFragment extends DialogFragment                            implements DatePickerDialog.OnDateSetListener {    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {        // Use the current date as the default date in the picker        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);        // Create a new instance of DatePickerDialog and return it        return new DatePickerDialog(getActivity(), this, year, month, day);    }    public void onDateSet(DatePicker view, int year, int month, int day) {        // Do something with the date chosen by the user    }}


 


Android的DatePickerDialog類詳解,

www.cnblogs.com/xiang1992/p/3411242.html 這是我以前寫的例子,希望對你有協助

android 開發文檔裡面有詳細的類和方法說明:developer.android.com/...g.html
 
android DatePickerDialog怎設定日期選擇範圍

  package com.solide;
  import java.util.Calendar;
  import android.app.Activity;
  import android.app.TimePickerDialog;
  import android.app.TimePickerDialog.OnTimeSetListener;
  import android.os.Bundle;
  import android.text.format.DateFormat;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TimePicker;
  public class TestTimeTickerActivity extends Activity implements OnClickListener{
  /** Called when the activity is first created. */
  private Calendar c;
  private Button button;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button)findViewById(R.id.button);
  button.setOnClickListener(this);
  c = Calendar.getInstance();
  c.setTimeInMillis(System.currentTimeMillis());

  }
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  int hour = c.get(Calendar.HOUR_OF_DAY);
  int minute = c.get(Calendar.MINUTE);
  new TimePickerDialog(this, new OnTimeSetListener() {
  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  // TODO Auto-generated method stub
  }
  }
  , hour, minute, DateFormat.is24HourFormat(this)).show();

  }
  }

  去試試...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.