【问题标题】:How to use AlertDialog to prompt for PIN如何使用 AlertDialog 提示输入 PIN
【发布时间】:2014-07-28 19:46:18
【问题描述】:

因此,我进行了大量研究,试图为 Hello World 游戏应用程序实现一个简单的 PIN 身份验证系统。不幸的是,我认为有些事情仍然在我的脑海中。

就我目前的代码而言,它会显示对话框并且不允许将其取消。但是,EditText 字段不再出现,这违背了要求 PIN 的目的。我认为出于布局目的需要链接的 XML 文件,但是,Android Studio 似乎不接受对此类文件的调用(即pinauth.setView(findViewById(R.layout.dialog_pin));,我认为这不是正确的结构。

您能提供的任何帮助将不胜感激。整个方法的代码贴在下面。


public void auth(View v)
{

// The logout method works fine, since no PIN is necessary.
if(users.getBoolean("activeLogin", false) == true) successLogout();
    else
    {
        // Get selected user and pull SharedPreferences to get stored PIN.
        final String activeUser = userList.getSelectedItem().toString();
        scores = getSharedPreferences(activeUser, 0);

        // If a PIN is found on file, launch AlertDialog to prompt for PIN. No XML file is linked to the AlertDialog at this time.
        if(scores.getInt("pin", 123456789) != 123456789)
        {
            AlertDialog.Builder pinAuth = new AlertDialog.Builder(this);
            pinAuth.setTitle("PIN required.");
            pinAuth.setMessage("Please enter your pin.");
            pinAuth.setCancelable(false);
            pinAuth.setView(findViewById(R.layout.dialog_pin))
            final EditText pin = new EditText(this);
            pin.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);

            // These checks seem to work ok.
            pinAuth.setPositiveButton("Login", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(pin.getText().toString().isEmpty())
                    {
                        makeToast("Sorry, no pin was entered.", Toast.LENGTH_SHORT);
                        return;
                    }
                    else if(Integer.valueOf(pin.getText().toString()) != scores.getInt("pin", 123456789))
                    {
                        makeToast("Sorry, that pin was incorrect. Try again.", Toast.LENGTH_SHORT);
                        return;
                    }
                    else
                        successLogin(activeUser);

                }
            });

            // Show the AlertDialog.
            pinAuth.show();
        }

        // if the account has no PIN
        else successLogin(activeUser);
    }
}

附带说明,PIN 最多只能是 8 个数字字符,因此 123456789 是不可能首先创建的 PIN,因此检查 PIN 的条件语句不等于所述数字不应干扰操作。

【问题讨论】:

    标签: java android xml android-edittext android-alertdialog


    【解决方案1】:

    您需要先inflate 自定义布局。

    AlertDialog.Builder pinAuth = new AlertDialog.Builder(this);
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.dialog_pin, null);
    
    pinAuth.setTitle("PIN required.");
    pinAuth.setCancelable(false);
    pinAuth.setView(view);
    
    final EditText pin = (EditText) view.findViewById(R.id.whateverThisEditTextIdIs);
    pin.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
    

    请注意,我还更改了您引用/创建EditText 对象的方式。我还取消了 pinAuth.setMessage() 的使用,因为您的自定义布局可能会显示该消息。

    【讨论】:

    • 完美运行。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2018-10-17
    • 2023-03-19
    相关资源
    最近更新 更多