android日期区间选择器
时间: 2023-10-09 22:09:20 浏览: 148
DataRangePicker-日期区间选择器
Android提供了DatePicker和CalendarView两个控件,可以用于日期选择,但是它们都只能选择单个日期,无法选择日期区间。如果需要实现日期区间选择器,可以使用第三方库或自定义控件来实现。
以下是一个简单的自定义控件实现日期区间选择器的示例代码:
```java
public class DateRangePicker extends LinearLayout {
private TextView mStartDateTextView;
private TextView mEndDateTextView;
private DatePickerDialog mStartDatePickerDialog;
private DatePickerDialog mEndDatePickerDialog;
public DateRangePicker(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.date_range_picker, this, true);
mStartDateTextView = view.findViewById(R.id.start_date_text_view);
mEndDateTextView = view.findViewById(R.id.end_date_text_view);
mStartDateTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showStartDatePickerDialog();
}
});
mEndDateTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showEndDatePickerDialog();
}
});
}
private void showStartDatePickerDialog() {
if (mStartDatePickerDialog == null) {
mStartDatePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
mStartDateTextView.setText(getFormattedDate(calendar.getTime()));
}
}, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
}
mStartDatePickerDialog.show();
}
private void showEndDatePickerDialog() {
if (mEndDatePickerDialog == null) {
mEndDatePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
mEndDateTextView.setText(getFormattedDate(calendar.getTime()));
}
}, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
}
mEndDatePickerDialog.show();
}
private String getFormattedDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
return sdf.format(date);
}
public String getStartDate() {
return mStartDateTextView.getText().toString();
}
public String getEndDate() {
return mEndDateTextView.getText().toString();
}
}
```
在布局文件中可以使用该自定义控件:
```xml
<com.example.daterangepicker.DateRangePicker
android:id="@+id/date_range_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
```
在代码中可以使用以下方法获取选择的日期区间:
```java
DateRangePicker dateRangePicker = findViewById(R.id.date_range_picker);
String startDate = dateRangePicker.getStartDate();
String endDate = dateRangePicker.getEndDate();
```
阅读全文