简体   繁体   English

Android:如何在触摸外部时关闭 DatePicker DialogFragment?

[英]Android: how to dismiss a DatePicker DialogFragment when touching outside?

I have a working DatePickerFragment that extends DialogFragment.我有一个扩展 DialogFragment 的工作 DatePickerFragment。 I set up a DatePickerDialog in onCreateDialog() and then tried to add:我在 onCreateDialog() 中设置了一个 DatePickerDialog,然后尝试添加:

"picker.setCanceledOnTouchOutside(true);" “picker.setCanceledOnTouchOutside(真);”

I am testing on a device with Android 8.0 Oreo and nothing happens when touching outside the DatePicker dialog.我正在使用 Android 8.0 Oreo 的设备上进行测试,在 DatePicker 对话框之外触摸时没有任何反应。 I am using androidx.appcompat.app.AppCompatActivity as my BaseActivity and androidx.fragment.app.DialogFragment for the DialogFragment;我使用 androidx.appcompat.app.AppCompatActivity 作为我的 BaseActivity 和 androidx.fragment.app.DialogFragment 作为 DialogFragment;

Code:代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    DatePickerDialog picker = new DatePickerDialog(getActivity(),this,year,month,day);
    **picker.setCanceledOnTouchOutside(true);**
    return picker;

This is the Activity code that creates the Fragment:这是创建片段的活动代码:

DatePickerFragment newFragment = new DatePickerFragment();
// tried the below also, with no luck
**newFragment.setCancelable(true);**
newFragment.show(getSupportFragmentManager(), "datePicker");

I also tried the below in the DialogFragment, also with no luck:我还在 DialogFragment 中尝试了以下内容,但也没有运气:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   ...
   getDialog().setCanceledOnTouchOutside(true);
   ... 
   }

and:和:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (getDialog() != null) {
        getDialog().setCanceledOnTouchOutside(true);
    }
    return super.onCreateView(inflater, container, savedInstanceState);
}    

I referenced this post for possible answers: How to dismiss a DialogFragment when pressing outside the dialog?我参考了这篇文章以获得可能的答案: How to dismiss a DialogFragment when pressing outside the dialog? . . What am I missing here?我在这里错过了什么?

if you want to dismiss Dialog that extends DialogFragment, write如果你想关闭扩展 DialogFragment 的 Dialog,写

setCancelable(true);设置取消(真);

inside onCreateView.在 onCreateView 里面。 The dialog will dismiss when you touching outside.当您触摸外部时,对话框将关闭。

example code:示例代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     setCancelable(true);
     return super.onCreateView(inflater, container, savedInstanceState);
}

did you try to set the dialog to cancelable您是否尝试将对话框设置为可取消

picker.setCancelable(true);

I thought I have similar problem, but it seems in my case that dialog has some shadow padding around it and I had to click outside of it very close to the edge of the screen to cancel it.我以为我有类似的问题,但在我的情况下,对话框似乎有一些阴影填充,我不得不在它的外部非常靠近屏幕边缘的地方单击以取消它。

This is how i create my dialog:这就是我创建对话框的方式:

val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)

val dpd = context?.let {
    DatePickerDialog(it, DatePickerDialog.OnDateSetListener { _, pickedYear, pickedMonth, pickedDay ->

         //do something with picked date.

    }, year, month, day)
}

dpd?.setCanceledOnTouchOutside(true)
dpd?.show()

Try to set setCanceledOnTouchOutside(true) in your implementation and try to dismiss it on tap very close to the edge of the screen, maybe test it on some big screen emulator like Pixel 3 XL.尝试在您的实现中设置setCanceledOnTouchOutside(true)并尝试在非常靠近屏幕边缘的地方点击关闭它,也许在一些大屏幕模拟器(如 Pixel 3 XL)上测试它。 Now i know that this is not a solution and you need to handle all kind of devices and screen sizes, but I want you to verify that you might have same problem as me: that dialog will be canceled on touch outside, but this "outside" is not so obvious and it might be a real problem.现在我知道这不是解决方案,你需要处理所有类型的设备和屏幕尺寸,但我希望你确认你可能遇到与我相同的问题:该对话框在触摸外部时取消,但是这个“外部" 不是那么明显,这可能是一个真正的问题。

Just add setCancelable(true) in your Dialog onCreate method or in constructor只需在 Dialog onCreate 方法或构造函数中添加 setCancelable(true)

Try to use the default DatePickerDialog from android which is default close when selecting out side the dialog.尝试使用Android的默认日期PickerDialog,在选择对话框的一侧时,默认值关闭。

Try this is still issue let me know will send the proper code for same 尝试这仍然是问题让我知道将发送相同的正确代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM