Android Studio - 在 TextView 上显示 dateRangePicker 中的选定日期
Android Studio - Show selected dates from dateRangePicker on TextView
有谁知道如何在 TextView 上显示从 dateRangePicker 中选择的日期?一旦用户从对话框中选择了日期,此函数应该会显示选定的日期范围。
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cuti);
btnDate = findViewById(R.id.btnDate);
pilihDate = findViewById(R.id.mvPilihTarikh);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.clear();
Pair<Long, Long> selectionDates=null;
Long today = MaterialDatePicker.todayInUtcMilliseconds();
calendar.setTimeInMillis(today);
calendar.roll(Calendar.MONTH, Calendar.JANUARY);
Long january = calendar.getTimeInMillis();
calendar.roll(Calendar.MONTH, Calendar.DECEMBER);
Long december = calendar.getTimeInMillis();
CalendarConstraints.Builder constraintBuilder = new CalendarConstraints.Builder();
constraintBuilder.setValidator(DateValidatorPointForward.now());
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());
MaterialDatePicker<?> picker = builder.build();
picker.show(getSupportFragmentManager(), builder.toString());
builder.setTitleText("PILIH TARIKH");
builder.setSelection(selectionDates);
builder.setCalendarConstraints(constraintBuilder.build());
MaterialDatePicker materialDatePicker = builder.build();
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
materialDatePicker.show(getSupportFragmentManager(), materialDatePicker.toString());
}
});
}
你可以看看这个教程。它使用 material 日期选择器。
https://www.geeksforgeeks.org/material-design-date-picker-in-android/
final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
// handle select date button which opens the
// material design date picker
mPickDateButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// getSupportFragmentManager() to
// interact with the fragments
// associated with the material design
// date picker tag is to get any error
// in logcat
materialDatePicker.show(getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
}
});
// now handle the positive button click from the
// material design date picker
materialDatePicker.addOnPositiveButtonClickListener(
new MaterialPickerOnPositiveButtonClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onPositiveButtonClick(Object selection) {
// if the user clicks on the positive
// button that is ok button update the
// selected date
mShowSelectedDateText.setText("Selected Date is : " + materialDatePicker.getHeaderText());
// in the above statement, getHeaderText
// is the selected date preview from the
// dialog
}
});
有谁知道如何在 TextView 上显示从 dateRangePicker 中选择的日期?一旦用户从对话框中选择了日期,此函数应该会显示选定的日期范围。
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cuti);
btnDate = findViewById(R.id.btnDate);
pilihDate = findViewById(R.id.mvPilihTarikh);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.clear();
Pair<Long, Long> selectionDates=null;
Long today = MaterialDatePicker.todayInUtcMilliseconds();
calendar.setTimeInMillis(today);
calendar.roll(Calendar.MONTH, Calendar.JANUARY);
Long january = calendar.getTimeInMillis();
calendar.roll(Calendar.MONTH, Calendar.DECEMBER);
Long december = calendar.getTimeInMillis();
CalendarConstraints.Builder constraintBuilder = new CalendarConstraints.Builder();
constraintBuilder.setValidator(DateValidatorPointForward.now());
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());
MaterialDatePicker<?> picker = builder.build();
picker.show(getSupportFragmentManager(), builder.toString());
builder.setTitleText("PILIH TARIKH");
builder.setSelection(selectionDates);
builder.setCalendarConstraints(constraintBuilder.build());
MaterialDatePicker materialDatePicker = builder.build();
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
materialDatePicker.show(getSupportFragmentManager(), materialDatePicker.toString());
}
});
}
你可以看看这个教程。它使用 material 日期选择器。
https://www.geeksforgeeks.org/material-design-date-picker-in-android/
final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
// handle select date button which opens the
// material design date picker
mPickDateButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// getSupportFragmentManager() to
// interact with the fragments
// associated with the material design
// date picker tag is to get any error
// in logcat
materialDatePicker.show(getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
}
});
// now handle the positive button click from the
// material design date picker
materialDatePicker.addOnPositiveButtonClickListener(
new MaterialPickerOnPositiveButtonClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onPositiveButtonClick(Object selection) {
// if the user clicks on the positive
// button that is ok button update the
// selected date
mShowSelectedDateText.setText("Selected Date is : " + materialDatePicker.getHeaderText());
// in the above statement, getHeaderText
// is the selected date preview from the
// dialog
}
});