java 显示输入警报对话框

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 显示输入警报对话框相关的知识,希望对你有一定的参考价值。

/**
 * Dialog Type Enum
 */
private enum DialogType {
    FULL_NAME,
    EMAIL,
    PHONE,
    PASSWORD
}
    
/**
 * Show Input Alert Dialog
 *
 * @param dialogType dialog type
 */
private void showInputAlertDialog(final DialogType dialogType) {
    // Create builder
    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(mActivityReference,
            R.style.AppTheme_AlertDialog);
    } else {
        builder = new AlertDialog.Builder(mActivityReference);
    }

    // Set the dialog title
    builder.setTitle(selectTitle(dialogType));

    // Inflate the layout
    LayoutInflater inflater = LayoutInflater.from(mActivityReference);
    final View dialogView = inflater.inflate(R.layout.layout_dialog_input_text, null);
    builder.setView(dialogView);

    // Edit Text
    mInputEditText = (EditText) dialogView.findViewById(R.id.dialog_edit_text);
    createEditText(dialogType);

    // Remove error if user change the text
    mInputEditText.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) {
            mInputEditText.setError(null);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    // Set up the buttons
    builder.setPositiveButton(mContext.getString(R.string.save), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    builder.setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    AlertDialog dialog = builder.create();

    // Open soft keyboard
    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
        WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

        // Override Positive Button
        @Override
        public void onShow(DialogInterface dialog) {
            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    // Positive Button Click
                    String value = mInputEditText.getText().toString();
                    handlePositiveButton(value, dialogType);
                }
            });
        }
    });

    // Show dialog
    dialog.show();
    mInputEditText.requestFocus();
}

以上是关于java 显示输入警报对话框的主要内容,如果未能解决你的问题,请参考以下文章

java 显示软键盘警报对话框。字体:https://stackoverflow.com/questions/12997273/alertdialog-with-edittext-open-soft-

用户输入太长,警报对话框无法处理

单击列表视图时如何显示警报对话框

单击列表视图时如何显示警报对话框

在颤动中自动在应用程序主屏幕加载上显示警报对话框

Flutter - 自定义警报对话框未显示