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

3
我正在尝试在编辑按钮中添加登录功能。我想要实现的是,当用户选择一个项目进行编辑,然后点击编辑按钮时,会弹出一个窗口询问该人的用户名和密码。我尝试创建了一个登录对话框,但总是失败。
这是我的代码:
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.");
                    }
    }

当我点击“确认”按钮时,它会显示“用户名或密码不正确”的消息。但是我已经输入了正确的用户名和密码。有什么帮助吗?

1
请分享您的dbUser.Login(username, password);函数代码。 - Arslan Anwar
你在代码中哪里比较用户输入的用户名和密码与记录中的用户名和密码? - Siddharth Lele
2个回答

4

一个小时后,我成功解决了自己的问题。所以我想分享这个答案,如果有人遇到类似于我之前遇到的问题,他们可以将其作为很好的参考。

我的做法是,尝试使用我的登录类代码。但我不再要求用户名了。我只问密码。但对于用户名,我从先前的登录中获取Intent。就像,您使用用户名“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.");
                }
}

就是这样。我感谢那些回答我的问题并给我一点帮助的人。我希望这篇文章也能帮助其他人。谢谢大家!


2
当您旋转屏幕时会发生什么?目前的建议是使用对话框片段。但两年后,这个答案已经不再相关。 - elcuco

0

看起来您没有正确获取用户名,就像密码一样,因为您正在使用这个。

  final String username = getIntent().getStringExtra("USERNAME");

但是你在Dialog EditText字段中输入用户名称,就像密码一样。你应该使用这个来获取输入的用户名

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

同时,在查询数据库之前,您应该验证用户名和密码是否为空。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接