本文介绍了防止在 Android 屏幕旋转时关闭对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图防止在 Activity 重新启动时关闭使用警报构建器构建的对话框.

I am trying to prevent dialogs built with Alert builder from being dismissed when the Activity is restarted.

如果我重载 onConfigurationChanged 方法,我可以成功地做到这一点并将布局重置为正确的方向,但我失去了edittext的粘性文本功能.因此,在解决对话框问题时,我创建了这个 edittext 问题.

If I overload the onConfigurationChanged method I can successfully do this and reset the layout to correct orientation but I lose sticky text feature of edittext. So in solving the dialog problem I have created this edittext problem.

如果我从编辑文本中保存字符串并在 onCofiguration 更改中重新分配它们,它们似乎仍然默认为初始值,而不是在旋转之前输入的值.即使我强制无效似乎也会更新它们.

If I save the strings from the edittext and reassign them in the onCofiguration change they still seem to default to initial value not what was entered before rotation. Even if I force an invalidate does seem to update them.

我真的需要解决对话框问题或编辑文本问题.

I really need to solve either the dialog problem or the edittext problem.

感谢您的帮助.

推荐答案

如今避免此问题的最佳方法是使用 DialogFragment.

The best way to avoid this problem nowadays is by using a DialogFragment.

创建一个扩展 DialogFragment.覆盖 onCreateDialog 并返回旧的 DialogAlertDialog.

Create a new class which extends DialogFragment. Override onCreateDialog and return your old Dialog or an AlertDialog.

然后你可以用 DialogFragment.show(fragmentManager, tag).

Then you can show it with DialogFragment.show(fragmentManager, tag).

这是一个带有 Listener:

Here's an example with a Listener:

public class MyDialogFragment extends DialogFragment {

    public interface YesNoListener {
        void onYes();

        void onNo();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof YesNoListener)) {
            throw new ClassCastException(activity.toString() + " must implement YesNoListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.dialog_my_title)
                .setMessage(R.string.dialog_my_message)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((YesNoListener) getActivity()).onYes();
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((YesNoListener) getActivity()).onNo();
                    }
                })
                .create();
    }
}

在您调用的 Activity 中:

And in the Activity you call:

new MyDialogFragment().show(getSupportFragmentManager(), "tag"); // or getFragmentManager() in API 11+

这个答案有助于解释其他三个问题(及其答案):

This answer helps explain these other three questions (and their answers):

这篇关于防止在 Android 屏幕旋转时关闭对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:13