使用自定义对话框匹配密码

时间:2012-11-15 08:01:05

标签: android dialog

我想使用自定义对话框进行引脚验证。这是我的代码:

public void fetchUI()
{
    add=(Button)findViewById(R.id.pinButton);

    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            final EditText input = new EditText(MainActivity.this);
            input.setTransformationMethod(PasswordTransformationMethod.getInstance());
            alert.setView(input);
            alert.setTitle("Type Your PIN");
            alert.setIcon(R.drawable.ic_launcher);
            alert.setMessage("Please Type Your PIN  below to Authenticate");

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            });

           alert.show();
        }
    });
}

现在我想这样做:如果我点击确定并输入正确的PIN,那么对话框就会消失。否则,如果我点击OK,它就不会消失。 我怎样才能做到这一点? 任何帮助将不胜感激。

6 个答案:

答案 0 :(得分:5)

    final String CORRECT_PIN = "123123"; // Should come from somewhere else than a local variable
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            if (CORRECT_PIN.equals(input.getText().toString()) {
                dialog.dismiss();
            } else {
                input.setError(getString(R.string.error_incorrect_pin));
            }    
        }
    });

编辑:上面的代码提供了处理验证的正确方法。但是,为了防止在单击按钮后取消对话框,您需要在自定义对话框按钮中包含自己的按钮。

其他可能性:仅在引脚正确时启用“确定”按钮。您需要在EditText和onTextChange方法中添加文本更改侦听器,请执行以下操作:

input.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {               
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(CORRECT_PIN.equals(input.getText().toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

答案 1 :(得分:2)

点击PositiveButton或NegativeButton时,AlertDialog总是会解散。

您要做的是将自己的“确定”和“取消”按钮添加到AlertDialog的视图中,并在这些按钮上设置侦听器。 所以不要只是打电话......

alert.setView(input);

...您还要添加按钮。像这样:

LinearLayout ll = new LinearLayout(context);
ll.addView(input);
ll.addView(okButtonYouInstansiatedYourself);
ll.addView(cancelButtonYouInstansiatedYourself);
alert.setView(ll);

okButtonYouInstansiatedYourself.b.setOnClickListener(...);
cancelButtonYouInstansiatedYourself.b.setOnClickListener(...);

答案 2 :(得分:2)

由于各种相关问题 我改变了与对话框相同的方法

我对此事的解决方案是

  

使用2个活动

  • 1父母
  • 2孩子

从父母那里打电话给孩子,按照你的要求去做 但是在menifest文件中将这些内容添加到您的子活动中 您的活动将表现为对话框

  

机器人:主题= “@机器人:风格/ Theme.Dialog”

答案 3 :(得分:1)

您可以将一个onShowListener添加到AlertDialog,然后您可以覆盖该按钮的onClickListener。

final AlertDialog d = new AlertDialog.Builder(context)
        .setView(v)
        .setTitle(R.string.my_title)
        .setPositiveButton(android.R.string.ok,
                new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface d, int which) {
                        //Do nothing here. We override the onclick
                    }
                })
        .setNegativeButton(android.R.string.cancel, null)
        .create();

d.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Do something

                //Dismiss once everything is OK.
                d.dismiss();
            }
        });
    }
});

来源:How to prevent a dialog from closing when a button is clicked

答案 4 :(得分:0)

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            String val = input.getText().toString();  /// this is your input editbox value

            }
        });

答案 5 :(得分:0)

您可以使用SharedPreference保存用户名和密码。您也可以通过SharedPreference检查您的密码和用户名。