当日期选择器返回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");
}
});
}
答案 0 :(得分:0)
在我的情况下,需要设置多个字段,因此我将它们作为函数中的参数进行传递。设置日期后,只需调用时间选择器功能即可。
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();
}