Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中设置登录对话框?_Android_Login_Android Alertdialog - Fatal编程技术网

如何在Android中设置登录对话框?

如何在Android中设置登录对话框?,android,login,android-alertdialog,Android,Login,Android Alertdialog,我试图在编辑按钮中添加登录名。我想做的是,当用户选择要编辑的项目,然后点击编辑按钮,弹出窗口将显示询问该用户的用户名和密码。我试着创建一个登录对话框,但一直失败 这是我的密码: public void btn_edit_click(View v){ LayoutInflater li = LayoutInflater.from(context); View prompt = li.inflate(R.layout.prompts, null); A

我试图在编辑按钮中添加登录名。我想做的是,当用户选择要编辑的项目,然后点击编辑按钮,弹出窗口将显示询问该用户的用户名和密码。我试着创建一个登录对话框,但一直失败

这是我的密码:

public void btn_edit_click(View v){
        LayoutInflater li = LayoutInflater.from(context);
        View prompt = li.inflate(R.layout.prompts, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setView(prompt);
        final String username = getIntent().getStringExtra("USERNAME");
        final EditText user = (EditText) prompt.findViewById(R.id.loginEmail);
        final EditText pass = (EditText) prompt.findViewById(R.id.loginPassword);
        final TextView msg = (TextView) prompt.findViewById(R.id.login_error);
        final String password = pass.getText().toString();
        user.setText(getIntent().getStringExtra("USERNAME"));
        if(this.selected_website != null){
            alertDialogBuilder.setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {   
                    DBAdapter dbUser = new DBAdapter(PasswordActivity.this);
                    dbUser.open();
                    if(dbUser.Login(username, password))
                    {
                        show_add_layout();

                    }
                    else
                    {
                        msg.setText("Username or Password is incorrect");
                    }
                }
                });

                    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });
                    alertDialogBuilder.show();  
                    }                   
                    else{
                        show_mesg("Please select item to edit.");
                    }
    }

当我点击OK按钮时,它会告诉我“用户名或密码不正确”。但是我输入了正确的用户名和密码。有什么帮助吗

似乎您没有像passwor那样正确地获取用户名,因为您正在使用它

  final String username = getIntent().getStringExtra("USERNAME");
但您正在为userEmail的对话框EditText字段中输入用户名和密码。你应该用这个来获取输入的用户名

final String username  = user.getText().toString();

您还应该验证用户名Passowrd是否为空,然后再在DB中检查它。

一个小时后,我设法解决了自己的问题。所以我想和大家分享这个答案,如果有人遇到我之前遇到的问题,他们可以把这个作为一个很好的参考

我所做的是,我尝试使用登录类的代码。但我不再要求用户名了。我问的只是密码。但是对于用户名,我从上一次登录中得到了意图。这就像,你用用户名“john316”登录,密码是“123abc”,然后你将被重定向到主类。在这里,您可以在列表视图中选择一个项目,当您单击“编辑”时,会出现一个弹出窗口,询问您的密码以进行验证。但是,任何密码都不起作用。它将从数据库和您登录的用户名(即“john316”)调用密码。如果正确,您将被重定向到编辑类。如果它是错误的,它会显示“密码不正确”,并会阻止您访问该页面。要缩短此说明,请参见以下代码:

public void btn_edit_click(View v){
    LayoutInflater li = LayoutInflater.from(context);
    View prompt = li.inflate(R.layout.prompts, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setView(prompt);
    final String username = getIntent().getStringExtra("USERNAME");
    final EditText pass = (EditText) prompt.findViewById(R.id.loginPassword);
    final TextView msg = (TextView) prompt.findViewById(R.id.login_error);

    if(this.selected_website != null){
        alertDialogBuilder.setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {   
                String password = pass.getText().toString();

                try{
                    if(username.equals(getIntent().getStringExtra("USERNAME")) && password.length() > 0)
                    {
                        DBAdapter dbUser = new DBAdapter(PasswordActivity.this);
                        dbUser.open();

                        if(dbUser.Login(username, password))
                        {
                            show_add_layout();
                        }else{
                            msg.setText("Password is incorrect");
                        }
                        dbUser.close();
                    }

                }catch(Exception e)
                {
                    Toast.makeText(PasswordActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
            });

                alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();

                    }
                });
                alertDialogBuilder.show();  
                }                   
                else{
                    show_mesg("Please select item to edit.");
                }
}

这就是它的工作原理。我感谢那些回答我问题并给我一点帮助的人。我希望这个也能帮助其他人。谢谢大家

请共享您的dbUser.Login(用户名、密码);功能代码。您在代码中的什么位置将用户输入的用户名和密码与记录中的用户名和密码进行比较?旋转屏幕时会发生什么情况?当前的建议是使用对话框片段。2年后,这个答案不再适用。