DatePickerDialog を使ってみた

DatePickerDialog は
java.lang.Object
  ↳ android.app.Dialog
    ↳ android.app.AlertDialog
      ↳ android.app.DatePickerDialog
を使います

というわけで、DatePickerDialog をいじってみた

サンプルプログラム
  1. import java.util.Calendar;  
  2. import android.app.Activity;  
  3. import android.app.DatePickerDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6. import android.widget.DatePicker;  
  7.   
  8. public class DatePickerDialogTest extends Activity {  
  9.   final Calendar calendar = Calendar.getInstance();  
  10.   // カレンダーから現在の '年' を取得  
  11.   int mYear = calendar.get(Calendar.YEAR);  
  12.   // カレンダーから現在の '月' を取得  
  13.   int mMonth = calendar.get(Calendar.MONTH);  
  14.   // カレンダーから現在の '日' を取得  
  15.   int mDay = calendar.get(Calendar.DAY_OF_MONTH);  
  16.   
  17.   DatePickerDialog datePickerDialog;  
  18.   
  19.   @Override  
  20.   public void onCreate(Bundle savedInstanceState) {  
  21.     super.onCreate(savedInstanceState);  
  22.     setContentView(R.layout.main);  
  23.   
  24.     // DatePickerDialog の日付が変更されたときに呼び出されるコールバックを登録  
  25.     DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {  
  26.       public void onDateSet(  
  27.           DatePicker view,   
  28.           int year,  
  29.           int monthOfYear,  
  30.           int dayOfMonth) {                  
  31.         mYear = year;         // '年' を取得  
  32.         mMonth = monthOfYear; // '月' を取得  
  33.         mDay = dayOfMonth;    // '日' を取得  
  34.       }  
  35.     };  
  36.   
  37.     // DatePickerDialog の作成  
  38.     datePickerDialog = new DatePickerDialog(  
  39.       this,     // 第1引数 : Context  
  40.       listener, // 第2引数 : DatePickerDialog.OnDateSetListener  
  41.       mYear,    // 第3引数 : 年  
  42.       mMonth,   // 第4引数 : 月  
  43.       mDay      // 第5引数 : 日  
  44.     );  
  45.   
  46.     // Dialog の Positive Button を設定  
  47.     datePickerDialog.setButton(  
  48.       DialogInterface.BUTTON_POSITIVE,  
  49.         "Positive",   
  50.         new DialogInterface.OnClickListener() {  
  51.           public void onClick(DialogInterface dialog, int which) {  
  52.             // Positive Button がクリックされた時の動作  
  53.           }  
  54.         }      
  55.     );  
  56.   
  57.     // Dialog の Negative Button を設定  
  58.     datePickerDialog.setButton(  
  59.       DialogInterface.BUTTON_NEGATIVE,   
  60.       "Negative",   
  61.       new DialogInterface.OnClickListener() {  
  62.         public void onClick(DialogInterface dialog, int which) {  
  63.           // Negative Button がクリックされた時の動作  
  64.         }  
  65.       }  
  66.     );  
  67.     
  68.     // Dialog の Neutral Button を設定  
  69.     datePickerDialog.setButton(  
  70.       DialogInterface.BUTTON_NEUTRAL,   
  71.       "Neutral",   
  72.       new DialogInterface.OnClickListener() {  
  73.         public void onClick(DialogInterface dialog, int which) {  
  74.           // Neutral Button がクリックされた時の動作  
  75.         }  
  76.       }  
  77.     );  
  78.   
  79.     // DatePickerDialog の表示  
  80.     datePickerDialog.show();  
  81.   }  
  82. }  

プログラムを実行すると...


こんな感じ!

0 件のコメント:

コメントを投稿