创建函数以使用DatePickerDialog和TimePickerDiaglog来捕获用户输入日期时间

时间:2015-12-18 07:35:35

标签: android mysql dialog datetimepicker

我正在尝试编写一个函数来捕获用户输入Datetime并存储在字符串值中。我成功创建DatePickerDialog,但TimePickerDialog。我收到错误"无法解析构造函数..." (尽管使用与DatePickerDialog相同的代码。非常感谢任何帮助。谢谢

这是我的功能代码

public void showDatePicker(View view) {
        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);

        int mHour = c.get(Calendar.HOUR);
        int mMinute = c.get(Calendar.MINUTE);

        final TextView selectDate = (TextView) findViewById(R.id.selectDate);


        DatePickerDialog datePickerDialog = new DatePickerDialog(DateTimePicker.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // dd.MM.yyyy
                selectDate.setText(dayOfMonth + "." + (monthOfYear + 1) + "." + year);
            }
        }, mYear, mMonth, mDay);
        datePickerDialog.show();

        TimePickerDialog timePickerDialog = new TimePickerDialog(DateTimePicker.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
                selectDate.append(hourOfDay + "." +minute);
            }
        },mHour,mMinute);
        timePickerDialog.show();
}

还有一个问题是如何将字符串$ selectDate转换为Timestamp并存储到远程MySQL数据库?

1 个答案:

答案 0 :(得分:0)

根据Timepicker dialog documentation

公共建设者

TimePickerDialog(Context context, TimePickerDialog.OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView)

创建一个新的时间选择器对话框。

TimePickerDialog(Context context, int themeResId, TimePickerDialog.OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView)

使用指定的主题创建新的时间选择器对话框。

所以,请遵循这个,你需要另一个类型为boolean的参数来创建TimePickerDialog对象。

e.g,

TimePickerDialog timePickerDialog = new TimePickerDialog(DateTimePicker.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
                selectDate.append(hourOfDay + "." +minute);
            }
        },mHour,mMinute,false);