java - 在空字符串输入上停用警报对话框肯定按钮

标签 java android android-alertdialog validating

我只想在用户输入不为空时启用 alertDialog 肯定按钮,如果用户输入为空则禁用它,这是用于验证目的的切换之王,用户输入不能为空。这是我的代码,即使我输入字符串按钮也不会激活。

buildInfos.setPositiveButton(android.R.string.ok, new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        infosDesc = inputInfos.getText().toString();
        Log.i("DESC", infosDesc);
        drawBetween2LastPoints(getAlertColor(alertType), "title", infosDesc);
    }
});

AlertDialog buildInfosDialog = buildInfos.create();
buildInfosDialog.show();
if(infosDesc.isEmpty()){
    buildInfosDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}

最佳答案

在上面的代码中,您仅在显示对话框后立即检查该字段是否为空。您必须查看 EditText 的内容以进行更改。为此,您可以添加 TextWatcher到现场,使用它的 addTextChangedListener(TextWatcher)-method .

TextWatcher 中,覆盖 afterTextChanged(Editable) 方法,每次字段内容更改(添加/删除内容)时都会调用该方法。在其中,检查 EditText 中是否有任何内容。如果有,请激活按钮。如果没有,请将其停用。

这里是一个例子实现:

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button button = new Button(this);
        button.setText("Show Dialog");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });

        setContentView(button);
    }

    private void showDialog(){
        // Create the field to show in the Dialog:
        final EditText field = new EditText(this);

        // Now create the Dialog itself.
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("Enter something")
                .setPositiveButton("O.K.", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Main.this, "Submitted with \""+field.getText().toString()+"\"",
                                Toast.LENGTH_LONG).show();
                    }
                }).setCancelable(true).setView(field)
                .create();

        // The TextWatcher will look for changes to the Dialogs field.
        field.addTextChangedListener(new TextWatcher() {
            @Override public void beforeTextChanged(CharSequence c, int i, int i2, int i3) {}
            @Override public void onTextChanged(CharSequence c, int i, int i2, int i3) {}

            @Override
            public void afterTextChanged(Editable editable) {
                // Will be called AFTER text has been changed.
                if (editable.toString().length() == 0){
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                } else {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                }
            }
        });

        // Show the Dialog:
        dialog.show();
        // The button is initially deactivated, as the field is initially empty:
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    }
}

关于java - 在空字符串输入上停用警报对话框肯定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17513521/

相关文章:

android - onShowCustomView 的解决方法

android - 在 alertDialog 中使用微调器时出现空指针异常

android - 再次显示相同的 AlertDialog

android - onClickListener 中的 AlertDialog

java - 线程模板

java - 将 CSV 导入 Apache Ignite

java - 带有自定义注释的抽象类

java - Android apt 从 1.4 升级到 1.7 版本

android - 使用HmsMessageService(HMS推送套件)无法在华为设备上获取通知

android - 为什么工作管理器中的 worker 仍然处于 ENQUEUED 状态?