2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidのpicker dialog

Last updated at Posted at 2018-10-25

#効果図

picker効果.gif
picker効果2.gif

#DatePickerDialog
###日付を選択する(最大値、最小値指定できる)

    // 日付情報の初期設定
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int monthOfYear = calendar.get(Calendar.MONTH);
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    // 最大値設定
    Calendar maxDate = Calendar.getInstance();
    maxDate.set(year, monthOfYear, dayOfMonth);
    maxDate.add(Calendar.DATE, +20);
    // 最小値設定
    GregorianCalendar minDate = new GregorianCalendar();
    minDate.set(year, monthOfYear, dayOfMonth);
    // 日付設定ダイアログの作成
    DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
            DateSetListener_expected_date, year, monthOfYear,
            dayOfMonth);
    DatePicker datePicker = datePickerDialog.getDatePicker();
    if (datePicker != null) {
        datePicker.setMaxDate(maxDate.getTimeInMillis());
        datePicker.setMinDate(calendar.getTimeInMillis());
    }
    // 日付設定ダイアログの表示
    datePickerDialog.show();

#TimePickerDialog
###時間を選択する

    int hour = 9;
    int minute = 00;
    final TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay,
                                      int minuteOfDay) {
                    // 時間表示をHH:mmに変換
                    String time = String.format("%02d:%02d", hourOfDay,
                            minuteOfDay);
                    L.d("time="+time);
                }
            }, hour, minute, true);
    timePickerDialog.show();

#独自タイトル定義
###AlertDialog.Builder+DatePicker

    int hour=0;
    int minute=0;
    final DatePicker dp = new DatePicker(this);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("独自タイトル");
    builder.setView(dp);
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    builder.setNegativeButton(android.R.string.cancel, null);
    builder.create().show();

#都道府県
###NumberPicker+AlertDialog.Builder

    NumberPicker mNumberPicker = new NumberPicker(this);
    mNumberPicker.setDisplayedValues(mCities);
    mNumberPicker.setMinValue(0);
    mNumberPicker.setMaxValue(mCities.length - 1);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("日本の都道府県");
    builder.setView(mNumberPicker);
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    builder.setNegativeButton(android.R.string.cancel, null);
    builder.create().show();

#まとめ
###日付の場合
DatePickerDialogを優先的に考える。タイトルなど定義したい場合はDatePicker+AlertDialog.Builderにする
###時間の場合は
TimePickerDialogを優先的に考える。タイトルなど定義したい場合はTimePicker+AlertDialog.Builderにする
###数字、都道府県など、1つpickerの場合は
NumberPicker+AlertDialog.Builderにする
###複雑の場合は
LayoutInflater+AlertDialog.Builderにする

#ソースコード 
###いいねも忘れずに

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?