Simple username password code in android not worki

2019-09-20 08:05发布

问题:

This question already has an answer here:

  • String comparison - Android 16 answers

In android studio im making a simple username and password code that toasts something if both the values in the username and password edit text views are certain values ("cake" and "robot" for instance) however when I test it and enter the correct username and password values im being told that I have entered the wrong match?

Here is the main activity

public class MainActivity extends AppCompatActivity {

EditText usernameET;
EditText passwordET;
Button SignIn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    usernameET = (EditText) findViewById(R.id.username);
    passwordET = (EditText) findViewById(R.id.password);
    SignIn = (Button) findViewById(R.id.signin);
}

public void SignIn(View view) {

    String username = usernameET.getText().toString();
    String password = passwordET.getText().toString();

    if(username == "cake" && password == "robot"){
        Toast.makeText(this, "You Signed In", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "Invalid Login", Toast.LENGTH_SHORT).show();
    }
}

}

main activity layout:

 <EditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/password"
    android:layout_alignStart="@+id/password"
    android:layout_marginBottom="15dp"
    android:hint="@string/username"
    android:inputType="text" />

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

    android:hint="@string/password"
    android:inputType="text" />

<Button
    android:id="@+id/signin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_in"
    android:onClick="SignIn"
    android:layout_below="@+id/password"
    android:layout_centerHorizontal="true" />

Why is this happening?

回答1:

I think you have to write

username.equals("cake") && password.equals("robot")

Reason is that you have to check the content of the edittext not the reference

when you use == it checks reference in the memory whereas you have to check the data.



回答2:

String username = usernameET.getText().toString();
String password = passwordET.getText().toString();

"==" matches the reference id of the Strings. so,the String username is an object in the heap whose reference_id will be different from the String "cake" whereas the .eqauls() method of the String class compares the content of the string. So , in your case use

if(username.equals("cake") && password.equals("robot")){
   Toast.makeText(this, "You Signed In",Toast.LENGTH_SHORT).show();
}else{
   Toast.makeText(this, "Invalid Login", Toast.LENGTH_SHORT).show();
}

for more reference http://www.studytonight.com/java/string-handling-in-java.php



回答3:

Reeplace username == "cake" && password == "robot" with username.equals("cake") && password.equals("robot")



回答4:

You are comparing string.. so you need to use .equals

public void SignIn(View view) {

    String username = usernameET.getText().toString();
    String password = passwordET.getText().toString();

    if(username.equals("cake") && password.equals("robot")){
        Toast.makeText(this, "You Signed In", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "Invalid Login", Toast.LENGTH_SHORT).show();
    }
}


回答5:

In java, a==b is used to compare 2 references, not the objects themselves.

so if you have 2 strings that you want to compare, use the equals() method on String. For e.g , username.equals("cake") && password.equals("robot")



回答6:

Change your if condition like

 if(username.equalsIgnoreCase("cake") && password.equalsIgnoreCase("robot")