【问题标题】:Match password using custom dialog box使用自定义对话框匹配密码
【发布时间】:2012-11-15 08:01:05
【问题描述】:

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

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 单击“确定”,那么一个对话框就会消失。否则,如果我单击确定,它不会消失。 我怎样才能做到这一点? 任何帮助将不胜感激。

【问题讨论】:

    标签: android dialog


    【解决方案1】:
        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));
                }    
            }
        });
    

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

    其他可能性:仅当 pin 正确时才启用 OK 按钮。您需要在 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) {
        }
    });
    

    【讨论】:

    • @BinoyBabu,我认为 OP 想要这样。
    • @BinoyBabu,我现在明白你想说什么了。即使引脚错误,对话框仍然消失。是的,我怀疑这是正确的答案。
    • 是的,这两种情况下对话框都会消失。
    • 抱歉,您确实需要创建一个自定义对话框(更好的是,使用对话框片段)。
    【解决方案2】:

    AlertDialog 在单击 PositiveButton 或 NegativeButton 时总是关闭。

    您要做的是将您自己的“确定”和“取消”按钮添加到 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(...);
    

    【讨论】:

    • 好方法。欧普应该试试这个。
    【解决方案3】:

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

    我对这件事的解决方案是

    使用 2 个活动

    • 1 个家长
    • 2 个孩子

    从父母那里打电话给孩子并按照您的要求做工作 但是在清单文件中将这些东西添加到您的孩子活动中 您的活动将表现得像对话框

    android:theme="@android:style/Theme.Dialog"

    【讨论】:

      【解决方案4】:

      您可以向 AlertDialog 添加一个 onShowListener,然后您可以覆盖按钮的 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

      【讨论】:

        【解决方案5】:
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    String val = input.getText().toString();  /// this is your input editbox value
        
                    }
                });
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2013-12-10
            • 2015-05-21
            • 2013-06-24
            • 2016-09-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多