如何创建有两个日期选择器自定义对话框? [英] How can I create a custom dialog with two datepicker?

查看:142
本文介绍了如何创建有两个日期选择器自定义对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学Android作为一种业余爱好,我想有两个日期选择器创建一个对话框

I just started to learn Android as a hobby and I would like to create a dialog with two datepicker

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

我怎样才能从对话框中选择的值?有没有一种可能性,包括OK /对话框中会自动取消按钮?

How can I get the selected values from the dialog? Is there a possibility to include the OK/Cancel button automatically on the dialog?

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

Is there a library which has such a functionality(Start and end date/period selection)?

推荐答案

这也可能是最好的阅读有关 对话框 拾荒者 第一。

It's probably best to read about Dialogs and Pickers first.

至于实施,可以有两个按钮:一个显示日期选择器的开始日期,另一个用于结束日期

As for the implementation, you can have two buttons: One to show a date picker for the start date and another for the end date.

编辑:如果你真的想显示2日期选择在1对话框,在这里有一个如何做到这一点的例子。首先,创建一个自定义XML布局。

If you really want to show 2 date pickers in 1 dialog, here's an example of how to do it. First, create a custom XML layout.

/res/layout/custom_date_picker.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>

下一步是使用上述布局在对话框:

Next is to use the above layout in a dialog:

// 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()

这篇关于如何创建有两个日期选择器自定义对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆