2

What I really want is what Windows calls a modal dialog box, i.e. one that must be closed by the user before they can do anything else. How can I get that effect in Android? It's particularly important for the first time someone runs my app because I want them to accept some terms and conditions, but I've got other cases where I need that as well.

At present I have a static class in my activity class as shown below, and I'm getting the dialog box to come up by calling the static method Create(...) on that class. But if the user just touches the activity behind the dialog box, or touches the search icon on the action bar, then the dialog box gets hidden. Can anyone help?

public static class SimpleDialog extends DialogFragment {
    private static final String DialogLayoutIdString = "DialogLayoutId";

    public static SimpleDialog Create(Activity activity, int Layoutid) {
        FragmentManager fm = activity.getFragmentManager();
        SimpleDialog about = new SimpleDialog();
        Bundle args = new Bundle();
        args.putInt(DialogLayoutIdString, Layoutid);
        about.setArguments(args);
        about.show(fm, "");
        return about;
    }

    private int LayoutId;
    private Activity activity;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Bundle args = getArguments();
        LayoutId = args.getInt(DialogLayoutIdString);
        setCancelable(cancellable);
        activity = getActivity();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        // various code to create the dialog box
        AlertDialog alert = builder.create();
        return alert;
    }
}
4

1 回答 1

3

称呼dialog.setCanceledOnTouchOutside(false);

这实际上曾经是默认设置。我不知道他们为什么改变它。请注意,这不会阻止他们只是点击后退按钮来关闭它 - 以停止该呼叫setCancelable(false);

于 2013-05-08T20:51:31.547 回答