java - 如果 EditText 为空,则阻止用户关闭警报对话框

标签 java android android-studio android-edittext android-alertdialog

我正在使用 AlertDialog Box 和 EditText,用户可以在其中输入他的名字。我想防止当 EditText 为空时警报对话框关闭。

下面是我的代码

 private void request_user_name() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter name:");

        final EditText input_field = new EditText(this);
        input_field.setText(sharedpreferences.getString("username",""));
        final SharedPreferences.Editor editor = sharedpreferences.edit();

        builder.setCancelable(false);
        builder.setView(input_field);
        String savedName = sharedpreferences.getString(username,"");
        input_field.setText(savedName);
        input_field.setSelection(input_field.getText().length());
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                name = input_field.getText().toString();
               

                editor.putString(username, name);
                editor.apply();
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                request_user_name();
            }
        });

        builder.show();
    }

我尝试插入

 if(TextUtils.isEmpty(name)) {
                input_field.setError("Your message");
                return;
            }

在AlertDialog Box里面是这样的,但是没用

 private void request_user_name() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Enter name:");

    final EditText input_field = new EditText(this);
    input_field.setText(sharedpreferences.getString("username",""));
    final SharedPreferences.Editor editor = sharedpreferences.edit();

    builder.setCancelable(false);
    builder.setView(input_field);
    String savedName = sharedpreferences.getString(username,"");
    input_field.setText(savedName);
    input_field.setSelection(input_field.getText().length());
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            name = input_field.getText().toString();
            if(TextUtils.isEmpty(name)) {
                input_field.setError("Your message");
                return;
            }


            editor.putString(username, name);
            editor.apply();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
            request_user_name();
        }
    });

    builder.show();
}

下面是图像文件,我有

enter image description here

以及我想要得到的图像

enter image description here

提前致谢。

最佳答案

使用setCancelable(false) .

设置一下即可

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Enter name:");
    builder.setCancelable(false);

关于java - 如果 EditText 为空,则阻止用户关闭警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767399/

相关文章:

java - 如何在不使用 AlertDialog.Builder 的情况下使用 ListView 创建 AlertDialog?

Android - 在 ListView 中管理点击和长按

android - 从 Android Studio gradle 任务构建和安装多个模块

java - 在 Android Studio [Java] 中使用内容解析器从图库中检索所有图像

java - Android Studio 未显示内联错误

java - 在 void 方法上使用 @NonNull 有什么作用?

java - 如何列出所有可用的 LookAndFeel 主题?

java - 理解java中的有界泛型。重点是什么?

android - onUpgrade备份表时_id冲突

android - 如何使 tablayout 文本大小相等?