在空字符串输入上停用警报对话框正向按钮

use*_*397 2 java android validating android-alertdialog

我想只在用户输入不为空时才启用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);
}
Run Code Online (Sandbox Code Playgroud)

Luk*_*uth 7

在上面的代码中,您只需在显示对话框后立即检查字段是否为空.你必须观看EditText改变的内容.为此,您可以TextWatcher使用addTextChangedListener(TextWatcher)-method将字段添加到字段中.

TextWatcher,覆盖afterTextChanged(Editable)-method,每当字段内容发生变化(添加/删除某些内容)时调用.在其中,检查是否有任何东西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);
    }
}
Run Code Online (Sandbox Code Playgroud)