web-dev-qa-db-ja.com

Datepickerとtimepicker-最大値と最小値を設定します

リマインダーアプリのようなものを作ろうとしています。ユーザーが現在ではない日付と時刻を選択できるようにしたい(少なくとも5分後)。また、ユーザーが遠すぎる日付(たとえば30日)を選択できないようにしたい。 datePickerとtimePickerを作成し、ボタンをクリックするとポップアップするようにしましたが、最小値と最大値を設定する方法が見つかりませんでした。

 public void showDateDialog() {
 btnDate = (Button) findViewById(R.id.buttonDate);

    btnDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
}
    public void showTimeDialog() {
    btnTime = (Button) findViewById(R.id.buttonTime);

    btnTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });
}
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            // create a new DatePickerDialog with values you want to show
            return new DatePickerDialog(this, datePickerListener, yearSet, monthSet, daySet);
        // create a new TimePickerDialog with values you want to show
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, timePickerListener, hourSet, minuteSet, false);
    }
    return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        yearSet = year;
        monthSet = monthOfYear + 1;
        daySet = dayOfMonth;
        btnDate.setText(new SimpleDateFormat("DD").format(daySet) + "-" + new SimpleDateFormat("MMM").format(monthSet) + "-" + yearSet);
    }
};
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {
        hourSet = hour;
        minuteSet = minute;
        btnTime.setText(hourSet + ":" + new SimpleDateFormat("MM").format(hourSet));
    }
};
9
Banana

これを試して:

btnDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        };

        Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);

        final DatePickerDialog datePickerDialog = new DatePickerDialog(
                Main23Activity.this, datePickerListener,
                mYear, mMonth, mDay);
        DatePicker datePicker = datePickerDialog.getDatePicker();

        c.add(Calendar.MONTH, +1);
        long oneMonthAhead = c.getTimeInMillis();
        datePicker.setMaxDate(oneMonthAhead);
        datePicker.setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();

    }
});

btnTime.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hour, int minute) {

            }
        };
        Calendar c = Calendar.getInstance();

        final TimePickerDialog timePickerDialog = new TimePickerDialog(Main23Activity.this,timePickerListener,
                c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE)+5,false);
        timePickerDialog.show();

    }
});

ただし、ユーザーが今日の日付を選択し、時刻が現在の時刻条件よりも短いかどうかを確認する必要があります。

DataOickerについては、これを試してください

public class MainActivity extends Activity {

    TextView txtDatePicker;
    Calendar cal,cal1;
    long maxDate;
    Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cal = Calendar.getInstance();
        cal1 = Calendar.getInstance();

        txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
        txtDatePicker.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
                    }
            }, cal.YEAR, cal.MONTH, cal.DAY_OF_MONTH);
            dialog.getDatePicker().setMinDate(System.currentTimeMillis());
            cal1.add(Calendar.YEAR, 5);
            dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
            dialog.show();
        }
    });
  }
}

TimePickerの場合、これを試してください exampel

5

日時ピッカーを組み合わせて、timePickerに条件を追加しました。

    public class TestActivity extends Activity {

        private Button mPickDate;
        private TextView mDateDisplay;
        private TextView mTimeDisplay;

        final Calendar c = Calendar.getInstance();
        private int mYear = c.get(Calendar.YEAR);
        private int mMonth = c.get(Calendar.MONTH);
        private int mDay = c.get(Calendar.DAY_OF_MONTH);
        private int mHour = c.get(Calendar.HOUR_OF_DAY);
        private int mMinute = c.get(Calendar.MINUTE);

        static final int TIME_DIALOG_ID = 1;
        static final int DATE_DIALOG_ID = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            });

            mDateDisplay = (TextView) findViewById(R.id.dateTextView);
            mPickDate = (Button) findViewById(R.id.datepicker);
            mTimeDisplay = (TextView) findViewById(R.id.timeTextView);

            mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    });

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

}

// Update date and time
private void updateDate() {
    mDateDisplay.setText(
            new StringBuilder()
                    .append(mDay).append("/")
                    .append(mMonth + 1).append("/")
                    .append(mYear).append(" "));
    showDialog(TIME_DIALOG_ID);
}

public void updateTime() {
    mTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));
}

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

// Generate DatePickerDialog and TimePickerDialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDate();
            }
        };

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;

                Calendar c2 = Calendar.getInstance();

                if (mYear == c2.get(Calendar.YEAR)
                        && mMonth == c2.get(Calendar.MONTH)
                        && mDay == c2.get(Calendar.DAY_OF_MONTH)
                        && (mHour < c2.get(Calendar.HOUR_OF_DAY) || (mHour == c2.get(Calendar.HOUR_OF_DAY) && mMinute <= (c2.get(Calendar.MINUTE)+10))
                    )
                ) {
                    Toast.makeText(TestActivity.this, "Set time at least 10 minutes from now", Toast.LENGTH_LONG).show();
                } else {
                    updateTime();
                }

            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
            c.add(Calendar.MONTH, +1);
            long oneMonthAhead = c.getTimeInMillis();
            datePickerDialog.getDatePicker().setMaxDate(oneMonthAhead);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            return datePickerDialog;

        case TIME_DIALOG_ID:
            TimePickerDialog timePickerDialog =
                    new TimePickerDialog(this,
                            mTimeSetListener, mHour, mMinute, false);

            return timePickerDialog;
    }
    return null;}
}
3
Banana