我正在开发基于位置的提醒的第一个应用程序,其中我添加了位置和日期和时间的任务,当我到达时我应该得到通知。在添加任务活动中,我有任务名称,位置,日期和时间按钮(当显示单击DatePickerDialog和TimePickerDialog时)和AddTask按钮。我想阻止用户从对话框中选择过去的日期和时间。我怎么能这样做??
public class AddTasks extends Activity {
TaskForm activityForm = new TaskForm();
private Button pPickDate;
private int pYear;
private int pMonth;
private int pDay;
public String date;
public String Loc;
public double lat;
public double lon;
private Button pPickTime;
private int mHour;
private int mMinute;
public String time;
private TimePickerDialog timePickerDialog;
private DatePickerDialog datePickerDialog;
Date selectedDate;
/** This integer will uniquely define the dialog to be used for displaying date picker.*/
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
/** Callback received when the user "picks" a date in the dialog */
private DatePickerDialog.OnDateSetListener pDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
updateDate();
displayDateToast();
}
};
private TimePickerDialog.OnTimeSetListener mTimeListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hour, int minute) {
mHour = hour;
mMinute = minute;
updateTime();
displayTimeToast();
}
};
/** Updates the date in the TextView */
private void updateDate() {
/** pDisplayDate.setText( */ date =
new StringBuilder()
// Month is 0 based so add 1
.append(pDay).append("/")
.append(pMonth + 1).append("/")
.append(pYear).append(" ").toString();
// date = pDisplayDate.getText().toString();
}
private void updateTime() {
/** pDisplayTime.setText( */ time =
new StringBuilder()
.append(mHour).append(":")
.append(mMinute).append("").toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
init();
/** Capture our View elements */
//pDisplayDate = (TextView) findViewById(R.id.displayDate);
pPickDate = (Button) findViewById(R.id.pickDate);
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** Capture our View elements */
//pDisplayTime = (TextView) findViewById(R.id.displayTime);
pPickTime = (Button) findViewById(R.id.pickTime);
/** Listener for click event of the button */
pPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
/** Get the current date and time */
final Calendar cal = Calendar.getInstance();
pYear = cal.get(Calendar.YEAR);
pMonth = cal.get(Calendar.MONTH);
pDay = cal.get(Calendar.DAY_OF_MONTH);
mHour = cal.get(Calendar.HOUR_OF_DAY);
mMinute = cal.get(Calendar.MINUTE);
}
/** Create a new dialog for date and time picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
datePickerDialog = new DatePickerDialog(this,
pDateSetListener,
pYear, pMonth, pDay);
return datePickerDialog;
case TIME_DIALOG_ID:
timePickerDialog = new TimePickerDialog(this,
mTimeListener,
mHour,mMinute,true) ;
return timePickerDialog;
}
return null;
}
public void add_task(View view) {
if(activityForm.getTaskNameInput().getText() != null &&
activityForm.getTaskLocInput() != null && activityForm.getTaskDateInput() != null &&
activityForm.getTaskTimeInput() != null) {
Task task = new Task();
task.setTaskNameString(activityForm.getTaskNameInput().getText()
.toString());
task.setTaskLocString(activityForm.getTaskLocInput());
task.setTaskLat(activityForm.getTaskLat());
task.setTaskLon(activityForm.getTaskLon());
task.setDateString(activityForm.getTaskDateInput());
task.setTimeString(activityForm.getTaskTimeInput());
persistTask(task);
Toast.makeText(getApplicationContext(), "SAVED=" + task.hashCode(),
Toast.LENGTH_LONG).show();
activityForm.getTaskNameInput().setText(" ");
Calendar c = Calendar.getInstance();
if (timePickerDialog != null) {
timePickerDialog.updateTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
}
if (datePickerDialog != null) {
datePickerDialog.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
}
else {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Attention");
dialog.setMessage("Please ensure you are setting all the values");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog.show();
}
}
class TaskForm {
private EditText taskNameInput;
private String taskLocInput;
private Double taskLat;
private Double taskLon;
private String taskTimeInput;
private String taskDateInput;
/**
* @return the taskNameInput
*/
public EditText getTaskNameInput() {
return taskNameInput;
}
/**
* @param taskNameInput
* the taskNameInput to set
*/
public void setTaskNameInput(EditText taskNameInput) {
this.taskNameInput = taskNameInput;
}
/**
* @return the taskLocInput
*/
public String getTaskLocInput() {
return taskLocInput;
}
/**
* @param taskLocInput
* the taskLocInput to set
*/
public void setTaskLocInput(String taskLocInput) {
this.taskLocInput = taskLocInput;
}
public Double getTaskLat(){
return taskLat;
}
public void setTaskLat(Double taskLat){
this.taskLat = taskLat;
}
public Double getTaskLon(){
return taskLon;
}
public void setTaskLon(Double taskLon){
this.taskLon = taskLon;
}
/**
* @return the taskDateInput
*/
public String getTaskDateInput(){
return taskDateInput;
}
/**
* @param taskDateInput
* the taskDateInput to set
*/
public void setTaskDateInput(String taskDateInput){
this.taskDateInput = taskDateInput;
}
/**
* @return the taskTimeInput
*/
public String getTaskTimeInput() {
return taskTimeInput;
}
/**
* @param taskTimeInput
* the taskTimeInput to set
*/
public void setTaskTimeInput(String taskTimeInput) {
this.taskTimeInput = taskTimeInput;
}
}
答案 0 :(得分:0)
您可以按当前日期在日历上设置minDate。