java - 如何使用 AlertDialog 提示输入 PIN

标签 java android xml android-edittext android-alertdialog

因此,我进行了大量研究,尝试为 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 码不等于所述数字不应干扰操作。

最佳答案

您需要首先膨胀自定义布局。

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() 的使用,因为您的自定义布局可能会显示该消息。

关于java - 如何使用 AlertDialog 提示输入 PIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25003121/

相关文章:

java - 测试一个巨大的分解方法及其内部辅助方法

android - OpenGL 与 OpenGL ES(GLchar 等)

xml - 从 Sql Server 列中的 XML 中提取数据

xml - 使用 XSLT 从 XML 中获取值并存储在变量中

java - 工作流程 LockProcess AEM - 不锁定 Assets /页面

java - 我怎样才能找出我的 Eclipse 项目实际需要的 200 个链接 JAR 中的哪一个?

android - 是否有可能找出 android 小部件是放在锁定屏幕还是主屏幕上?

android - 如何将图片保存在安全存储中并在 android 中访问它们?

java - IntelliJ Idea 标记红色 web.xml 效果很好

java - OneToMany JPA 注释