关闭 DialogFragment 上的 Outsideclick 无效 Android
Closing with Outsideclick on DialogFragment not working Android
我有一个 cutstom DialogFragment
向用户显示消息:
public class MensajeDialogFragment extends DialogFragment {
TextView mTvMensaje;
TextView mTvTitulo;
Button mBtnAceptar;
Button mBtnCancelar;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.layout_mensaje_dialog, null);
mTvTitulo = (TextView) dialogView.findViewById(R.id.tvTitulo);
mTvMensaje = (TextView) dialogView.findViewById(R.id.tvMensaje);
mBtnAceptar = (Button) dialogView.findViewById(R.id.btnAceptar);
mBtnCancelar = (Button) dialogView.findViewById(R.id.btnCancelar);
mTvTitulo.setText(getArguments().getString(getString(R.string.bundle_titulo), ""));
mTvMensaje.setText(getArguments().getString(getString(R.string.bundle_mensaje), ""));
mBtnAceptar.setText(getArguments().getString(getString(R.string.bundle_aceptar), ""));
mBtnCancelar.setText(getArguments().getString(getString(R.string.bundle_cancelar), ""));
builder.setView(dialogView);
getDialog().setCanceledOnTouchOutside(true);
return builder.create();
}
}
但是当我到达 getDialog().setCanceledOnTouchOutside(true)
时,我得到 NullPointerException
因为 getDialog()
正在返回 null
。
我做错了什么?我想在用户单击对话框外部时关闭对话框。
由于您正在使用 DialogFragment,因此不会在 DialogFragment 的 onCreate 上初始化对话框。
我相信 DialogFragment 默认情况下会在用户单击外部 it.You 时关闭,而不必明确声明它。如果你还想调用这个函数,那么DialogFragment有个函数叫
DialogFragment.setCancelable(boolean)
编辑
如果以上代码无效,您可以尝试调用
getDialog().setCanceledOnTouchOutside(true);
在 onCreateView 内部,因为 Dialog 对象将在 onCreateDialog 期间在 getLayoutInflater 上初始化。所以当它到达 onCreateView 对话框对象时将被初始化。
我有一个 cutstom DialogFragment
向用户显示消息:
public class MensajeDialogFragment extends DialogFragment {
TextView mTvMensaje;
TextView mTvTitulo;
Button mBtnAceptar;
Button mBtnCancelar;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.layout_mensaje_dialog, null);
mTvTitulo = (TextView) dialogView.findViewById(R.id.tvTitulo);
mTvMensaje = (TextView) dialogView.findViewById(R.id.tvMensaje);
mBtnAceptar = (Button) dialogView.findViewById(R.id.btnAceptar);
mBtnCancelar = (Button) dialogView.findViewById(R.id.btnCancelar);
mTvTitulo.setText(getArguments().getString(getString(R.string.bundle_titulo), ""));
mTvMensaje.setText(getArguments().getString(getString(R.string.bundle_mensaje), ""));
mBtnAceptar.setText(getArguments().getString(getString(R.string.bundle_aceptar), ""));
mBtnCancelar.setText(getArguments().getString(getString(R.string.bundle_cancelar), ""));
builder.setView(dialogView);
getDialog().setCanceledOnTouchOutside(true);
return builder.create();
}
}
但是当我到达 getDialog().setCanceledOnTouchOutside(true)
时,我得到 NullPointerException
因为 getDialog()
正在返回 null
。
我做错了什么?我想在用户单击对话框外部时关闭对话框。
由于您正在使用 DialogFragment,因此不会在 DialogFragment 的 onCreate 上初始化对话框。 我相信 DialogFragment 默认情况下会在用户单击外部 it.You 时关闭,而不必明确声明它。如果你还想调用这个函数,那么DialogFragment有个函数叫
DialogFragment.setCancelable(boolean)
编辑
如果以上代码无效,您可以尝试调用
getDialog().setCanceledOnTouchOutside(true);
在 onCreateView 内部,因为 Dialog 对象将在 onCreateDialog 期间在 getLayoutInflater 上初始化。所以当它到达 onCreateView 对话框对象时将被初始化。