java.lang.Object ↳ android.app.Dialog ↳ android.app.AlertDialog ↳ android.app.DatePickerDialogを使います
というわけで、DatePickerDialog をいじってみた
サンプルプログラム
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.widget.DatePicker;
- public class DatePickerDialogTest extends Activity {
- final Calendar calendar = Calendar.getInstance();
- // カレンダーから現在の '年' を取得
- int mYear = calendar.get(Calendar.YEAR);
- // カレンダーから現在の '月' を取得
- int mMonth = calendar.get(Calendar.MONTH);
- // カレンダーから現在の '日' を取得
- int mDay = calendar.get(Calendar.DAY_OF_MONTH);
- DatePickerDialog datePickerDialog;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // DatePickerDialog の日付が変更されたときに呼び出されるコールバックを登録
- DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
- public void onDateSet(
- DatePicker view,
- int year,
- int monthOfYear,
- int dayOfMonth) {
- mYear = year; // '年' を取得
- mMonth = monthOfYear; // '月' を取得
- mDay = dayOfMonth; // '日' を取得
- }
- };
- // DatePickerDialog の作成
- datePickerDialog = new DatePickerDialog(
- this, // 第1引数 : Context
- listener, // 第2引数 : DatePickerDialog.OnDateSetListener
- mYear, // 第3引数 : 年
- mMonth, // 第4引数 : 月
- mDay // 第5引数 : 日
- );
- // Dialog の Positive Button を設定
- datePickerDialog.setButton(
- DialogInterface.BUTTON_POSITIVE,
- "Positive",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // Positive Button がクリックされた時の動作
- }
- }
- );
- // Dialog の Negative Button を設定
- datePickerDialog.setButton(
- DialogInterface.BUTTON_NEGATIVE,
- "Negative",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // Negative Button がクリックされた時の動作
- }
- }
- );
- // Dialog の Neutral Button を設定
- datePickerDialog.setButton(
- DialogInterface.BUTTON_NEUTRAL,
- "Neutral",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // Neutral Button がクリックされた時の動作
- }
- }
- );
- // DatePickerDialog の表示
- datePickerDialog.show();
- }
- }
プログラムを実行すると...
こんな感じ!
0 件のコメント:
コメントを投稿