开发者

Help with password validation

I created a login application. But when I input a wrong password, the app won't give me any notification that the password was incorrect and it's because I didn't even put an开发者_运维百科y codes for it. I don't know what to put in order to achieve it. I'm a newbie here. Please help.

This is my code:

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();
        }
    }

}


You probably want an else condition on your inner if statement:

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.
}


The simplest think would be to show a Toast notification. You could do that in the else branch of the second (nested) if in your code above.


Hello schadi: i think you problem is this:EditText catch the click event as first as you click it that you should want to input some password.so,your code should always running at '"".equals(password)' statement. you may can use TextWatcher or something else to do your work.

Chinese guy,English is poor,i hope you can understand what i am saying :)


I suggest using a toast to notify the user. Like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜