如何创建一个带有两个日期选择器的自定义对话框?

6

我最近在业余时间学习Android开发,想创建一个包含两个日期选择器的对话框。

final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.data_picker_dialog);
dialog.setTitle(R.string.date_period_picker);
dialog.show();
return true;

如何从对话框中获取所选的值?是否可以自动将“确定/取消”按钮包括在对话框中?

是否有一个库具有这样的功能(开始和结束日期/期间选择)?


在对话框XML中添加两个日期选择器。使用OnDateChangedaListener监听更改。使用setNegativeButton设置“取消”和setPositiveButton设置“确定”。 - Milan
3个回答

13

建议先阅读有关对话框(Dialogs)选择器(Pickers)的内容。

至于实现方法,您可以拥有两个按钮:一个用于显示开始日期的日期选择器,另一个用于结束日期。

编辑:如果您真的想在一个对话框中显示两个日期选择器,这是一个如何实现的示例。首先,创建自定义的XML布局。

/res/layout/custom_date_picker.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <DatePicker
        android:id="@+id/dpStartDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:calendarViewShown="false" />

    <DatePicker
        android:id="@+id/dpEndDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:calendarViewShown="false" />

</LinearLayout>

接下来是在对话框中使用上述布局:

// These variables will hold the date values later
private int startYear, startMonth, startDay, endYear, endMonth, endDay;

/**
 * Displays the start and end date picker dialog
 */
public void showDatePicker() {
    // Inflate your custom layout containing 2 DatePickers
    LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
    View customView = inflater.inflate(R.layout.custom_date_picker, null);

    // Define your date pickers
    final DatePicker dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate);
    final DatePicker dpEndDate = (DatePicker) customView.findViewById(R.id.dpEndDate);

    // Build the dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(customView); // Set the view of the dialog to your custom layout
    builder.setTitle("Select start and end date");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startYear = dpStartDate.getYear();
            startMonth = dpStartDate.getMonth();
            startDay = dpStartDate.getDayOfMonth();
            endYear = dpEndDate.getYear();
            endMonth = dpEndDate.getMonth();
            endDay = dpEndDate.getDayOfMonth();
            dialog.dismiss();
        }});

    // Create and show the dialog
    builder.create().show();
}

最后,您只需调用showDatePicker()即可显示此对话框。


2
这也是一个解决方案,但更好的解决方案是在一个对话框中同时拥有两者。 - Kicsi Mano
这个救了我的命..很棒的例子。 - ken

1
同样适用于您的布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
      android:id="@+id/datePicker1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

<DatePicker
      android:id="@+id/datePicker2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

0

你只需要在你的XML布局(data_picker_dialog)中创建日期选择器,然后通过ID获取数据


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接