创建函数以使用 DatePickerDialog 和 TimePickerDiaglog 来捕获用户输入的日期时间
Create function to use DatePickerDialog and TimePickerDiaglog to catch user input date time
我正在尝试编写一个函数来捕获用户输入 Date
和 time
并存储在字符串值中。我成功创建了 DatePickerDialog
,但创建了 TimePickerDialog
。我收到错误 "can not resolve constructor..."(尽管使用与 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 转换为时间戳并存储到远程 MySQL 数据库?
根据 Timepicker dialog documentation:
Public 构造函数
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)
创建一个具有指定主题的新时间选择器对话框。
所以,请按照此操作,您需要另一个布尔类型的参数来创建 TimePickerDialog
对象。
例如,
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);
我正在尝试编写一个函数来捕获用户输入 Date
和 time
并存储在字符串值中。我成功创建了 DatePickerDialog
,但创建了 TimePickerDialog
。我收到错误 "can not resolve constructor..."(尽管使用与 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 转换为时间戳并存储到远程 MySQL 数据库?
根据 Timepicker dialog documentation:
Public 构造函数
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)
创建一个具有指定主题的新时间选择器对话框。
所以,请按照此操作,您需要另一个布尔类型的参数来创建 TimePickerDialog
对象。
例如,
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);