0
我一直在嘗試設置一個時間選擇器對話框,用戶可以選擇時間,然後保存並將時間作爲字符串傳遞給下一個活動。如何設置按鈕單擊時彈出的TimePickerDialog?
我按照一些教程,併成功地設置DatePickerDialog,但我堅持讓時間選取器對話框工作。
這是我的嘗試:
@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));
}
我想修改我的代碼了一點,但一點紅的頭腦還是來了說{/(/;缺少
Attached a cap screen photo of the error
你能從控制檯發佈錯誤嗎? –
發表您的logcat – Abhishek
我試圖修改我的代碼,但有點紅色的心仍然出現說{/(/;缺少。 –