Android上的AlertDialog问题

时间:2015-10-16 09:48:28

标签: android alertdialog dialogfragment

在尝试显示自定义内容AlertDialog时,我遇到了一些奇怪的用户界面问题。该对话框要求用户输入一个名称,并且不允许他在没有这样做的情况下向前移动。这也是用户在活动开始时看到的第一件事。

有时候,在应用程序重新启动之后 - 让我说我在打开对话框时按下主页按钮,然后我重新打开应用程序,AlertDialog正在显示,但是父活动的布局是没有正确加载。它实际上保留了用户看到的上一个Activity的布局。更奇怪的是,这种布局几乎总是向后显示。您可以在here中看到更好的结果。在对话框后面,它应该是一个空白的白色布局,而是从“设置”应用程序中恢复启动器活动的“快照”。

正如官方文档所示,我将AlertDialog包裹在DialogFragment

public class NicknamePickerDialog extends DialogFragment {

    public static final String TAG = NicknamePickerDialog.class.getSimpleName();

    public interface NicknameDialogListener {
        void onNicknamePicked(String nickname);
        void onPickerCanceled();
    }

    private NicknameDialogListener mListener;
    private EditText mNicknameEditText;
    private Button mPositiveButton;

    public void setNicknameDialogListener(NicknameDialogListener listener) {
        mListener = listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Set the title
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.pick_nickname);

        // Inflate the custom content
        View dialogView = getActivity().getLayoutInflater().inflate(R.layout.nickname_dialog_layout, null);
        builder.setView(dialogView);

        mNicknameEditText = (EditText) dialogView.findViewById(R.id.nickname);

        builder.setPositiveButton(R.string.great, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (mListener != null) {
                    mListener.onNicknamePicked(mNicknameEditText.getText().toString());
                }
            }
        });

        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (mListener != null) {
                    mListener.onPickerCanceled();
                }
            }
        });

        final AlertDialog dialog = builder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                mPositiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
                mPositiveButton.setEnabled(false);
            }
        });

        mNicknameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }

            @Override
            public void afterTextChanged(Editable s) {
                mPositiveButton.setEnabled(s.length() != 0);
            }
        });

        return dialog;
    }
}

这是Activity代码

public class ChatActivity extends Activity implements NicknamePickerDialog.NicknameDialogListener {

    private String mNickname;
    private TextView mWelcomeTextView;
    private NicknamePickerDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_activity_layout);

        mWelcomeTextView = (TextView) findViewById(R.id.welcome);
        mDialog = new NicknamePickerDialog();
        mDialog.setNicknameDialogListener(this);
    }

    private void showNicknamePickerDialog() {
        mDialog.show(getFragmentManager(), NicknamePickerDialog.TAG);
    }

    @Override
    public void onNicknamePicked(String nickname) {
        mNickname = nickname;
        mWelcomeTextView.setText("Welcome " + nickname + "!");
    }

    @Override
    public void onPickerCanceled() {
        if (mNickname == null) {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mNickname == null) {
            showNicknamePickerDialog();
        };
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDialog.dismiss();
    }
}

起初我怀疑这可能是因为我在活动的DialogFragment回调中调用show的{​​{1}}方法(因为它可能太早了),但推迟了它迟到onCreate()并没有解决问题。方向更改时也会出现此问题,对话框背后的背景会变黑。我确信我做错了什么但我真的不知道那是什么。

1 个答案:

答案 0 :(得分:0)

我真的没有得到你想做的事。但是你做错了一件事是。

在类NicknamePickerDialog中覆盖方法OnCreateView()并执行以下操作

// Inflate the custom content
        View dialogView = getActivity().getLayoutInflater().inflate(R.layout.nickname_dialog_layout, null);
        builder.setView(dialogView);

        mNicknameEditText = (EditText) dialogView.findViewById(R.id.nickname);
   mNicknameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }

            @Override
            public void afterTextChanged(Editable s) {
                mPositiveButton.setEnabled(s.length() != 0);
            }
        });
return dialogView;

您的警报对话框也不起作用。更好地创建onCreateDialog()中的按钮和标题。

dialog.setTitle(R.string.pick_nickname);

希望这会奏效。