简体   繁体   中英

Text doesn't change in variable after pressing button in Android

For a little android project I'm making a loginscreen. I have variables, but they stay empty once I fill them in and press the button.

these are my variables:

private EditText email;
private EditText wachtwoord;
private Button loginButton;

here I fill them:

        email = (EditText)findViewById(R.id.emailInput);
        wachtwoord = (EditText)findViewById(R.id.wachtwoordInput);
        loginButton = (Button)findViewById(R.id.loginButton);
        
        }
}

and my method which always executes the catch code

    public void login(View view)
    {
        
        try {
        
        if(email.getText().toString() == "jolt_koens@hotmail.com" && wachtwoord.getText().toString() == "admin")
        {
            Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
            loginButton.setEnabled(false);
        }
        

Compare strings using either of these methods

method 1:

 if(((email.getText().toString().compareTo("jolt_koens@hotmail.com")==0) && (wachtwoord.getText().toString().compareTo("admin")==0))
 {
   do something
  }

method 2:

if(((email.getText().toString().equals("jolt_koens@hotmail.com") )&& (wachtwoord.getText().toString().equals("admin")))
    {
   do something
   }
public void login(View view)
    {

        try {

        if(email.getText().toString() .equals ("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equals( "admin"))
        {
            Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
            loginButton.setEnabled(false);
        }

Try the above code. Use .equals() instead of == To understand the difference between the two read this .

Change this line:

if(email.getText().toString() == "jolt_koens@hotmail.com" && wachtwoord.getText().toString() == "admin")

to this:

if(email.getText().toString().equals("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equals("admin"))
if(email.getText().toString().equalsIgnoreCase("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equalsIgnoreCase ("admin"))
        {
            Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
            loginButton.setEnabled(false);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM