android - 如何按 'cancel' 按钮?

标签 android android-alertdialog

我目前正在尝试使用 AlertDialogs。

目前,我的 AlertDialog 中有一个 EditText,并且希望将输入限制为仅整数。我使用了一个基本的 try 和 catch block 来避免应用程序因 NumberFormatException 而崩溃。

但是,我想对其进行设置,以便当用户尝试按下输入不正确的按钮时,输入不会注册并且不会取消对话框。

UpperLimitDialog.setPositiveButton(R.string.Positive_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        int RawInput = settings.getInt("UpperLimit", 12);
                        try {
                            RawInput = Integer.parseInt(input.getText().toString());
                        }catch (NumberFormatException se) {
                            Toast.makeText(SettingsMenu.this, "Non-Integer Value, No Input", Toast.LENGTH_SHORT).show();
                        //Here I want the app to not register the click and prevent the dialog box from closing.
                        }
                        editor.putInt("UpperLimit", RawInput);
                        editor.apply();
                        Toast.makeText(SettingsMenu.this, "Set Upper Limit to " + RawInput, Toast.LENGTH_SHORT).show();
                    }
                });

我可以使用什么方法来实现这一点?

最佳答案

这里的基本技巧是将对话框的 setCancelable 设置为 false 并仅在输入已验证时调用 dismiss()

更多信息在这里:AlertDialog with positive button and validating custom EditText

关于android - 如何按 'cancel' 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549675/

相关文章:

android - 收到短信后在 Android 中刷新 ListView 不起作用

android - 如何更改 ListPreference 弹出对话框的样式?

android - alertDialog 无法正常工作

android - 将自定义对话框 'header' 更改为 AlertDialog 'header'

java - BroadcastReceiver 中的 AlertDialog??可以吗?

java - 在另一个 Activity 中显示 Double

android - 如何使用 Volley 从服务器 url 将图像插入到 sqlite 中?

jquery - Phonegap dom 更新未立即反射(reflect)在显示中

android - Maps V2 如何旋转标记 - 旋转选项在哪里?

java - 如何将字符串数组传递给 AlertDialog builder.setItems()