【问题标题】:Android DialogFragment - Get reference to date pickerAndroid DialogFragment - 获取对日期选择器的引用
【发布时间】:2014-11-20 12:16:09
【问题描述】:

Android 文档告诉我们像这样实现日期选择器,扩展 DialogFragment:

public static class DatePickerFragment extends DialogFragment
                        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}

然后为了在我们的Activity中显示日期选择器对话框,我们调用这个方法:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

我需要知道的是,如何获得对对话框中使用的实际日期选择器的引用,以便我可以使用 .setMinDate() 和 .setMaxDate() 等方法?有什么建议吗?

【问题讨论】:

    标签: android dialog datepicker


    【解决方案1】:

    http://developer.android.com/reference/android/app/DatePickerDialog.html

    onCreateDialog(Bundle savedInstanceState) 中,您返回DatePickerDialog 上的引用。 return new DatePickerDialog(getActivity(), this, year, month, day);

    DatePickerDialog 有方法getDatePicker () 你可以使用它来获取DatePicker

    更新

    public static class DatePickerFragment extends DialogFragment
                        implements DatePickerDialog.OnDateSetListener {
    
    private DatePicker mDatePicker;
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
    
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
        mDatePicker = dialog.getDatePicker();
    
        return dialog;
    }
    
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
    
    public DatePicker getDatePicker() {
        return mDatePicker
    }
    

    }

    现在您可以访问此 datePicker。

    public void showDatePickerDialog(View v) {
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
    DatePicker picker = newFragment.getDatePicker();
    

    }

    【讨论】:

    • 我知道这种方法。我的活动中没有提及 DatePickerDialog。只有 DialogFragment - 在 showDatePickerDialog 方法中查看实例是如何创建的
    • 我添加了一些代码以便更好地理解。查看更新的答案。
    • 感谢您的帮助,但这不起作用。在 newFragment 上调用时无法解析方法 getDatePicker()。
    • 这是因为你写:DialogFragment newFragment = new DatePickerFragment(); 你应该使用:DatePickerFragment newFragment = new DatePickerFragment(); 我明天会检查这个,但我认为这应该可以。
    • 如何在向用户显示对话框之前使用 DatePickerDialog 中的 mDatePicker 对其进行修改(即,restrictMinDate)?我看到的问题是 mDatePicker 在调用 show() 之前将为空。有什么想法吗?
    【解决方案2】:

    我在StackOverflow answer 的帮助下解决了这个问题:

    public void showDatePickerDialog() {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
        getFragmentManager().executePendingTransactions();     // commits the show method from above
        DatePickerDialog dialog = (DatePickerDialog) newFragment.getDialog();
        DatePicker datePicker = (DatePicker) dialog.getDatePicker();
        Date now = new Date();
        datePicker.setMaxDate(now.getTime());
    }
    

    您需要在片段管理器上调用.executePendingTransactions()来停止异步添加对话框片段,否则getDialog()方法会给出空指针异常。

    我将它放在这里,以供将来可能遇到此问题的任何人使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多