我想做一个类似提醒应用程序的东西.我想允许用户选择现在不是的日期和时间(至少从现在起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)); } };
Dhinakaran T.. 7
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(); } });
您需要检查用户是否选择今天日期,然后时间小于当前时间条件.
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,请尝试以下示例