简体   繁体   中英

If and else statements not working in onClick() method in java

Hey I am a beginner to android studio and I have just started building android apps.Nowadays I am building an app using java in which user is required to create an account before using app.If user click Create Button and the required fields are empty or if the password and re-enter password don't match,respective toast messages have to be displayed on the screen.But if neither of those conditions happen,I simply want the user to move on to next Activity(on clicking Button).But the problem is when I tested the app,I entered all the required information in given fields and both passwords were also correct but instead of displaying next activity,my app was showing first toast message repeatedly.I' have been stuck here for a long time.I have tried to change if-else statements too but nothing worked.Can anybody help me how to run my app withput these issues?My code is listed below

package com.example.oneclickscanner;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AccountActivity extends AppCompatActivity {

    TextView accountLogo;
    EditText edtUsername,edtPassword,edtConfirmPassword;
    Button createButton,exitButton;

    String userName = edtUsername.getText().toString();
    String password = edtPassword.getText().toString();
    String confirmPassword = edtConfirmPassword.getText().toString();
    
    boolean enterRequiredFields = false;
    boolean passwordMatching = true;



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

        accountLogo = findViewById(R.id.accountLogo);

        edtUsername = findViewById(R.id.edtUsername);
        edtPassword = findViewById(R.id.edtPassword);
        edtConfirmPassword = findViewById(R.id.edtConfirmPassword);

        createButton = findViewById(R.id.createButton);
        exitButton = findViewById(R.id.exitButton);
        

        


        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(userName.equals("") || password.equals("") || confirmPassword.equals("")){
                    enterRequiredFields = true;
                }else if(!userName.equals("") || !password.equals("") || !confirmPassword.equals("")){
                    enterRequiredFields = false;
                }



                if(!password.equals(confirmPassword)){
                    passwordMatching = false;
                }else{
                    passwordMatching = true;
                }
                
                
                if(enterRequiredFields){
                    Toast.makeText(AccountActivity.this,"Please Enter the required Fields",Toast.LENGTH_SHORT).show();
                }else if(!passwordMatching) {
                    Toast.makeText(AccountActivity.this,"Password don't Match",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(AccountActivity.this,ModelAndLicenseInfo.class);
                    startActivity(intent);
                }

            }
        });


        exitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AccountActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

}

That's because the block of code that evaluates enterRequiredFields is inside the OnCreate block - that means it is evaluated on start up to true since nothing has been entered yet, and it never changes.

You should simply move the block that evaluates enterRequiredFields and passwordMatching inside the onClick for createButton , so that these are evaluated when the user clicks the button, and it should then work as expected.

UPDATE You need to bring the references for userName password and confirmPassword into the OnClick block too, like this:

package com.example.oneclickscanner;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AccountActivity extends AppCompatActivity {

    TextView accountLogo;
    EditText edtUsername,edtPassword,edtConfirmPassword;
    Button createButton,exitButton;

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

        accountLogo = findViewById(R.id.accountLogo);

        edtUsername = findViewById(R.id.edtUsername);
        edtPassword = findViewById(R.id.edtPassword);
        edtConfirmPassword = findViewById(R.id.edtConfirmPassword);

        createButton = findViewById(R.id.createButton);
        exitButton = findViewById(R.id.exitButton);     

        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            String userName = edtUsername.getText().toString();
            String password = edtPassword.getText().toString();
            String confirmPassword = edtConfirmPassword.getText().toString();
            boolean enterRequiredFields = false;
            boolean passwordMatching = true;

            if(userName.equals("") || password.equals("") || confirmPassword.equals("")){
                enterRequiredFields = true;
            }else if(!userName.equals("") || !password.equals("") || !confirmPassword.equals("")){
                enterRequiredFields = false;
            }



            if(!password.equals(confirmPassword)){
                passwordMatching = false;
            }else{
                passwordMatching = true;
            }
                
                
            if(enterRequiredFields){
                Toast.makeText(AccountActivity.this,"Please Enter the required Fields",Toast.LENGTH_SHORT).show();
            }else if(!passwordMatching) {
                Toast.makeText(AccountActivity.this,"Password don't Match",Toast.LENGTH_SHORT).show();
            }else {
                Intent intent = new Intent(AccountActivity.this,ModelAndLicenseInfo.class);
                startActivity(intent);
            }

        }
    });


        exitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AccountActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

}

Following @Jimmy's answer, you also need to modify your else-if condition, since only when userName , password and confirmPassword were not empty string, the condition enterRequiredFields should be made false .

From,

   else if(!userName.equals("") || !password.equals("") || !confirmPassword.equals("")){
        enterRequiredFields = false;
   }

To,

   else if(!userName.equals("") && !password.equals("") && !confirmPassword.equals("")){
        enterRequiredFields = false;
   }

    

So instead of cheacking your edittexts in onCreate, do it this way. Also use TextUtils to get accurate empty string checks from Edittext like this.

      createButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        if(TextUtils.isEmpty(userName)|| TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)){
        enterRequiredFields = true;
        }else if(!TextUtils.isEmpty(userName) || !TextUtils.isEmpty(password) || !TextUtils.isEmpty(confirmPassword)){
        enterRequiredFields = false;
        }



       if(!password.equals(confirmPassword)){
        passwordMatching = false;
       }else{
        passwordMatching = true;
    }
            if(enterRequiredFields){
                Toast.makeText(AccountActivity.this,"Please Enter the required Fields",Toast.LENGTH_SHORT).show();
            }else if(!passwordMatching) {
                Toast.makeText(AccountActivity.this,"Password don't Match",Toast.LENGTH_SHORT).show();
            }else {
                Intent intent = new Intent(AccountActivity.this,ModelAndLicenseInfo.class);
                startActivity(intent);
            }

        }
    });

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