在 DatePickerDialog 中设置最小日期
Set minimum Date in DatePickerDialog
我想从明天开始设置我的 DatePicker 对话框的最小日期。例如,如果今天是 2015 年 4 月 30 日,则它应该从 2015 年 5 月 1 日开始。
我的代码:
final Calendar myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
startDate.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
DatePickerDialog dialog = new DatePickerDialog(
SomeClassName.this, date, myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH));
dialog.show();
dialog.getDatePicker().setMinDate(
System.currentTimeMillis() - 10000);
}});
我分别面临2个问题
日期范围从当前日期开始,即 2015 年 4 月 30 日。
无论我通过滚动日期选择器选择什么日期,都只会设置 2015 年 4 月 30 日。
注意:日期选择器对话框会在编辑文本框发生触摸事件时弹出。
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the tommorrow date as the default date in the picker
final Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, 1);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dp = new DatePickerDialog(getActivity(), this, year, month, day);
dp.getDatePicker().setMinDate(c.getTimeInMillis());
return dp;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
显示日期选择器:
public void showDatePickerDialog() {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
欲了解更多信息,请访问:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker
虽然上面的建议是对的,但是我换了个方法。从我这边发布解决方案。
final Calendar myCalendar = Calendar.getInstance();
myCalendar.set(Calendar.DAY_OF_MONTH+1);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
startDate.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
DatePickerDialog dialog = new DatePickerDialog(
SomeClassName.this, date, myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH));
dialog.show();
dialog.getDatePicker().setMinDate(
myCalendar.getTimeInMillis());
}});
private void updateLabel()
{
//Do some stuff
}
我想从明天开始设置我的 DatePicker 对话框的最小日期。例如,如果今天是 2015 年 4 月 30 日,则它应该从 2015 年 5 月 1 日开始。
我的代码:
final Calendar myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
startDate.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
DatePickerDialog dialog = new DatePickerDialog(
SomeClassName.this, date, myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH));
dialog.show();
dialog.getDatePicker().setMinDate(
System.currentTimeMillis() - 10000);
}});
我分别面临2个问题
日期范围从当前日期开始,即 2015 年 4 月 30 日。
无论我通过滚动日期选择器选择什么日期,都只会设置 2015 年 4 月 30 日。
注意:日期选择器对话框会在编辑文本框发生触摸事件时弹出。
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the tommorrow date as the default date in the picker
final Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, 1);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dp = new DatePickerDialog(getActivity(), this, year, month, day);
dp.getDatePicker().setMinDate(c.getTimeInMillis());
return dp;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
显示日期选择器:
public void showDatePickerDialog() {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
欲了解更多信息,请访问:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker
虽然上面的建议是对的,但是我换了个方法。从我这边发布解决方案。
final Calendar myCalendar = Calendar.getInstance();
myCalendar.set(Calendar.DAY_OF_MONTH+1);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
startDate.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
DatePickerDialog dialog = new DatePickerDialog(
SomeClassName.this, date, myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH));
dialog.show();
dialog.getDatePicker().setMinDate(
myCalendar.getTimeInMillis());
}});
private void updateLabel()
{
//Do some stuff
}