在日期选择器中选择日期后如何打开时间选择器
Posted
技术标签:
【中文标题】在日期选择器中选择日期后如何打开时间选择器【英文标题】:How to open time picker after selecting date in date picker 【发布时间】:2020-02-21 11:53:49 【问题描述】:当日期选择器返回selected date
时,代码应自动显示时间选择器。当时间选择器返回时,活动应该"Toast"
选择的日期和时间。
FloatingActionButton dateFab = findViewById(R.id.datePicker);
dateFab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
//Open Date Picker Fragment
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListener(MainActivity.this);
newFragment.show(getSupportFragmentManager(),
"Pick the Date");
TimePickerFragment newFragment1 = new TimePickerFragment();
newFragment1.setListener(MainActivity.this);
newFragment1.show(getSupportFragmentManager(),
"Pick the Time");
);
【问题讨论】:
【参考方案1】:在我的情况下,它是多个字段,需要设置,因此我将它们作为函数中的参数传递。设置日期后只需调用 timepicker 函数。
private void showDatePickerDialog(final EditText etDates)
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);
DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
etDates.setText( (monthOfYear + 1) + "-" + dayOfMonth + "-" +year
showTimePickerDialog(etTime);
, mYear, mMonth, mDay);
datePickerDialog.setTitle("Select Date");
datePickerDialog.show();
private void showTimePickerDialog(final EditText etTime)
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(mContext, new TimePickerDialog.OnTimeSetListener()
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute)
etTime.setText(selectedHour + ":" + selectedMinute);
, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
【讨论】:
以上是关于在日期选择器中选择日期后如何打开时间选择器的主要内容,如果未能解决你的问题,请参考以下文章
在日期选择器中选择日期时,它不应接受等于年龄大于 20 岁的生日