Simple username password code in android not working [duplicate]

孤街浪徒 提交于 2020-01-26 04:55:34

问题


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")


来源:https://stackoverflow.com/questions/44900405/simple-username-password-code-in-android-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!