我在Android应用程序中工作,并使用DialogFragment显示对话框,我想使DialogFragment不可取消。我已将对话框cancelable属性设置为false,但仍然没有影响。
请查看我的代码并建议我一个解决方案。
public class DialogTest extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_test, container, true);
getDialog().requestWindowFeature(STYLE_NO_TITLE);
getDialog().setCancelable(false);
return view;
}
}
答案 0 :(得分:207)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_test, container, true);
getDialog().requestWindowFeature(STYLE_NO_TITLE);
getDialog().setCancelable(false);
return view;
}
而不是getDialog().setCancelable(false);
您必须直接使用setCancelable(false);
所以更新的答案将是这样的
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_test, container, true);
getDialog().requestWindowFeature(STYLE_NO_TITLE);
setCancelable(false);
return view;
}
答案 1 :(得分:47)
使用以下代码段
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string..alert_dialog_two_buttons_title);
newFragment.setCancelable(false);
newFragment.show(getFragmentManager(), "dialog");
}
如果要禁用外侧触摸对话框,请使用以下代码行
DialogFragment.getDialog().setCanceledOnTouchOutside(true);
答案 2 :(得分:28)
如果您使用警报构建器(并且可能在每种情况下都在DialogFragment中包装对话框)来帮助构建对话框,请不要使用getDialog()。setCancelable(false)或Dialog.setCancelable(false) 因为它无法正常工作。 使用setCancelable(false),如下面的代码所示,正如它在官方android文档中提到的那样:
public void setCancelable (boolean cancelable)
在API级别11中添加 控制显示的Dialog是否可取消。使用它而不是直接调用Dialog.setCancelable(boolean),因为DialogFragment需要根据它改变它的行为。"
参考:http://developer.android.com/reference/android/app/DialogFragment.html#setCancelable(boolean)
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_layout, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("in case you want use a title").setView(view);
AlertDialog alert = builder.create();
// alert.setCancelable(false); <-- dont' use that instead use bellow approach
setCancelable(false); <- press back button not cancel dialog, this one works fine
alert.setCanceledOnTouchOutside(false); <- to cancel outside touch
return alert;
}
答案 3 :(得分:1)
DialogFragment中的简单解决方案
已使用
dialog.setCanceledOnTouchOutside(false)
答案 4 :(得分:0)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
AlertDialog.Builder(activity!!).apply {
isCancelable = false
setMessage("Your message")
// your other adjustments
return this.create()
}
}
为我工作。
主要是在isCancelable = false
上使用setCancellable(false)
在override fun onCreateDialog()
之内。
答案 5 :(得分:0)
/**
* Control whether the shown Dialog is cancelable. Use this instead of
* directly calling {@link Dialog#setCancelable(boolean)
* Dialog.setCancelable(boolean)}, because DialogFragment needs to change
* its behavior based on this.
*
* @param cancelable If true, the dialog is cancelable. The default
* is true.
*/
DialogFragment.setCancelable(boolean cancelable) {
mCancelable = cancelable;
if (mDialog != null) mDialog.setCancelable(cancelable);
}