问题
Is it possible to popup a TimePicker when an EditText is clicked/touched?
I've tried looking for examples of this but I haven't found any. I want to use this time entered to set up an alarm, so it'd be great if someone could tell me how to do this and provide me of an example.
I'd prefer it if the Time was in 24 hour format instead of 12 hour format.
Thanks for any answers submitted.
回答1:
I will give you some tips to achieve that.
To show popup/dialog after EditText is clicked/touched you will find answer here: Android EditText onClickListener. Also is there discussed if popping dialog after clicking on EditText is standard/non-standard interface.
For TimePicker you can use system TimePickerDialog. You can find tutorial for TimePickerDialog here: http://www.pavanh.com/2013/04/android-timepicker-example.html
To use 24 hour format instead 12 hour format in TimePicker you can use following method:
timePicker.setIs24HourView(true)
, or if you use TimePickerDialog you will pas true in constructor.
Hope it will help you.
回答2:
This is the solution. I forgot about the true/false boolean and that's why it wasn't working when I tried something similar to the DatePicker code.
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);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
来源:https://stackoverflow.com/questions/17841404/edittext-pops-up-timepicker-for-alarm-reminder-system