帮助密码验证

时间:2011-01-29 06:04:28

标签: java android eclipse passwords

我创建了一个登录应用程序。但是当我输入错误的密码时,应用程序不会给我任何密码不正确的通知,因为我甚至没有为它添加任何代码。我不知道该怎么做才能实现它。我是新手。请帮忙。

这是我的代码:

public void onClick(View v)
{ 
    EditText passwordEditText = (EditText) findViewById(R.id.currentPass);
    SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
    String password = prefs.getString("password","");
    if("".equals(password))
    {
        Editor edit = prefs.edit();
        edit.putString("password",passwordEditText.getText().toString());
        edit.commit();
        StartMain();
    }
    else
    {
     if(passwordEditText.getText().toString().equals(password))
        {
             StartMain();
        }
    }

}

4 个答案:

答案 0 :(得分:2)

您可能希望在内部if语句中使用else条件:

if(passwordEditText.getText().toString().equals(password)) //checking if they put the right password?
    {
         StartMain(); //I assume this is starting the application
    }
else
{
    //Tell them the password was wrong.
}

答案 1 :(得分:1)

最简单的想法是展示Toast notification。您可以在上面代码中第二个(嵌套)else的{​​{1}}分支中执行此操作。

答案 2 :(得分:0)

你好schadi:     我认为你的问题是这样的:当你点击它时,EditText首先捕获click事件,你应该输入一些密码。所以,你的代码应该总是运行在'“”.equals(密码)'语句中。     你可以使用TextWatcher或其他东西来完成你的工作。

中国人,英语很差,我希望你能理解我在说什么:)

答案 3 :(得分:0)

我建议使用toast通知用户。像这样:

 else
    {
     if(passwordEditText.getText().toString().equals(password))
        {
             StartMain();
        }
    } else {
    Toast.makeText(v.getContext(), "Wrong password!", Toast.LENGTH_LONG).show();
}
相关问题