I have been trying to set up a time picker dialog where user can select the time, then save and pass the time as string to next activity.
I follow some tutorial and successfully set up the DatePickerDialog, but I am stuck at getting the Time Picker Dialog to work.
This is my attempt:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_blood_glucose_record);
mItemSelectedMessageTemplate =
getString(R.string.spinner_message_template);
Spinner spinner = (Spinner) findViewById(R.id.measured_time);
spinner.setOnItemSelectedListener(new SpinnerInfo());
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.TRADITIONAL_CHINESE);
findViewsById();
setDateTimeField();
timeFormatter = new SimpleDateFormat("hh:mm",Locale.TRADITIONAL_CHINESE);
findViewsById();
setTimeField();
editPatientID = (EditText) findViewById(R.id.editPatientID);
edit_sugar_con = (EditText) findViewById(R.id.edit_sugar_con);
}
private void setDateTimeField() {
btnSelectDate.setOnClickListener(this);
//editTime.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog = new DatePickerDialog(this, new android.app.DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
btnSelectDate.setText(dateFormatter.format(newDate.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
private void setTimeField(){
btnSelectTime.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
TimePickerDialog = new TimePickerDialog(this, new android.app.TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar newTime = Calendar.getInstance();
newTime.set(hourOfDay, minute);
btnSelectDate.setText(timeFormatter.format(newTime.getTime()));
}
}, newCalendar.get(Calendar.HOUR_OF_DAY), newCalendar.get(Calendar.MINUTE));
}
I tried to modified my code a bit but a little red mind still comes up saying {/(/; is missing.
wrong:
newTime.set(hourOfDay, minute);
right:
Calendar newTime = Calendar.getInstance();
newTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
newTime.set(Calendar.MINUTE, minute);
newTime.set(Calendar.SECOND, 0);
newTime.set(Calendar.MILLISECOND, 0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.