gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:48:58 24 4
gpt4 key购买 nike

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

这是我的代码:

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.");
}
}

当我单击“确定”按钮时,它会提示我“用户名或密码不正确”。但是我输入了正确的用户名和密码。有什么帮助吗?

最佳答案

一个小时后,我设法解决了自己的问题。所以我想分享这个答案,如果有人遇到我刚才遇到的问题,他们可以把它作为一个很好的引用。

我所做的是,我尝试使用登录类的代码。但是我不再要求用户名了。我问的只是密码。但是对于用户名,我从之前的登录中得到了 Intent。就像,您使用用户名“john316”登录,密码是“123abc”,然后您将被重定向到主类。在 ListView 中选择一个项目,当您单击编辑时,会弹出一个窗口,要求您输入密码进行验证。但是,任何密码都不起作用。如果正确,它将从数据库和您登录的用户名(“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.");
}
}

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

关于android - 如何在 Android 中设置登录对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14638438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com