Android 弹窗验证输入条件后,再关闭弹窗

代码笔记

        EditText inputServer = new EditText(this);
        inputServer.setHint("请输入密码");
        AlertDialog alertDialog = new AlertDialog.Builder(this)
        .setCancelable(false)
        .setMessage("XXXXX!")
        .setTitle("XXX!!!")
        .setIcon(android.R.drawable.ic_dialog_info)
        .setView(inputServer)
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        })
        .setPositiveButton("确定", null)
        .show();

        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = inputServer.getText().toString().trim();
                if(input.isEmpty()) {
                    inputServer.setHint("请输入密码!");
                    return;
                }
                if (input.equals(password)) {
                    alertDialog.dismiss();
                }else {
                    inputServer.setText("");
                    inputServer.setHint("密码错误,请联系管理员获取!");
                    return;
                }
            }
        });

 

你可能感兴趣的:(android开发)