简体   繁体   English

如何设置单击按钮时弹出的TimePickerDialog?

[英]How to set up the TimePickerDialog that pop up on button click?

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. 我遵循一些教程并成功设置了DatePickerDialog,但是我仍然无法让Time Picker对话框正常工作。

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. 我尝试了一些修改代码,但是仍然有些冒犯性的想法说{/(/;丢失了。

Attached a cap screen photo of the error 附加了错误的基本画面照片

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM