简体   繁体   English

If 和 else 语句在 java 中的 onClick() 方法中不起作用

[英]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嘿,我是 android 工作室的初学者,我刚刚开始构建 android 应用程序。现在我正在使用 java 构建应用程序如果密码和重新输入密码不匹配,则必须在屏幕上显示相应的敬酒消息。但如果这些情况都没有发生,我只是希望用户继续进行下一个活动(点击按钮)。但是问题是当我测试应用程序时,我在给定字段中输入了所有必需的信息,并且两个密码也是正确的,但是我的应用程序没有显示下一个活动,而是重复显示第一个 toast 消息。我已经卡在这里很长时间了.我也尝试更改 if-else 语句,但没有任何效果。有人可以帮我解决这些问题如何运行我的应用程序吗?我的代码列在下面

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.这是因为评估enterRequiredFields的代码块位于OnCreate块内 - 这意味着它在启动时评估为true ,因为尚未输入任何内容,并且它永远不会改变。

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.您应该简单地将评估enterRequiredFieldspasswordMatching的块移动到onClick中的createButton ,以便在用户单击按钮时评估这些块,然后它应该按预期工作。

UPDATE You need to bring the references for userName password and confirmPassword into the OnClick block too, like this:更新您还需要将用户名passwordconfirmPassword密码的引用带入userName OnClick block中,如下所示:

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 .按照@Jimmy 的回答,您还需要修改else-if条件,因为只有当userNamepasswordconfirmPassword不是空字符串时,条件enterRequiredFields应该设置为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.因此,不要在 onCreate 中检查您的编辑文本,而是这样做。 Also use TextUtils to get accurate empty string checks from Edittext like this.还可以使用 TextUtils 从 Edittext 中获取准确的空字符串检查,就像这样。

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

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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