我已经有一个DatePicker,当用户单击EditText字段时,它会弹出
eReminderDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//To show current date in the datepicker
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(AddReminder.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
selectedmonth = selectedmonth + 1;
eReminderDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select Date");
mDatePicker.show();
}
});
我曾尝试以类似方式进行TimePicker,但无法使其正常工作。这是我尝试使其工作的尝试
eReminderTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
eReminderTime.setText( ""selectedHour + ":" + selectedMinute);
}
}, hour, minute);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
像我为DatePicker所做的那样,是否不可能做到这一点?
单击此代码后,我什至尝试仅使TimePicker弹出。
eReminderTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
由于某种原因,当我将其输入Android Studio时,“showDialog”被剔除。
谁能给我有关我要去哪里的提示?还是我只需要使用自定义DialogFragment?
最佳答案
eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);
您错过了
+
和选定的小时之间的""
,因此setText
方法仅包含一个字符串,因此您需要连接所有部分(可能不需要使用第一引号)。eReminderTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
eReminderTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
那应该可以解决第二个错误,因为您没有提供最后一个参数。
TimePickerDialog Constructors
关于android - 单击EditText的TimePicker对话框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17901946/