search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

Android-如何建立建立2按鈕,選取日期+時間(DatePickerDialog、TimePickerDialog)到editText: – jashliao部落格

Android-如何建立建立2按鈕,選取日期+時間(DatePickerDialogTimePickerDialog)editText


 

 

注意:整個事件程式碼都必須放在

onCreate()


}

下面

……….

 

範例:

 

import android.widget.*;

import android.view.*;

 

import java.util.Calendar;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.app.TimePickerDialog;

 

 

 

 

 

  protected
void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_main);

  }

 

 

  //
button1 Date
日期 事件處理程序

  public
void button1_Click(View view) {

      showDialog(0);    

    

  }

  //
button2 Time
時間 事件處理程序

  public
void button2_Click(View view) {

      showDialog(1);

  }

 

  Calendar
dt = Calendar.getInstance();

 

  protected
Dialog onCreateDialog(int id) {

      switch (id) {

      case 0: // 傳回 日期
DatePickerDialog
對話方塊

       DatePickerDialog dDialog = new
DatePickerDialog(this,

          new DatePickerDialog.OnDateSetListener() {

           public void onDateSet(DatePicker view, int
year,

              int monthOfYear,  int dayOfMonth) {

             

       //程式碼寫在這裡                

       //=year, =monthOfYear+1, =dayOfMonth

 

         

             }                   

         }, dt.get(Calendar.YEAR),

           dt.get(Calendar.MONTH),

           dt.get(Calendar.DAY_OF_MONTH));

       return dDialog;

      

      

      case 1: // 傳回TimePickerDialog對話方塊

       TimePickerDialog tDialog = new
TimePickerDialog(this,

        new TimePickerDialog.OnTimeSetListener() {

        @Override

        public void onTimeSet(TimePicker view,

              int hourOfDay, int minute) {

        

       //程式碼寫在這裡

       //=hourOfDay, =minute

 

        }

       },
dt.get(Calendar.HOUR),dt.get(Calendar.MINUTE),true);

      return tDialog;

      }

      return null;

   }  //
end of Dialog onCreateDialog()  

 

 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦