首页 > 解决方案 > 在我的代码中,if 部分工作正常,但是 else 部分没有执行

问题描述

我正在尝试设置 OTP 验证

我已经尝试了 if 和 else 的许多可能性,但是,它并没有帮助。

 public class userLogin extends Activity {

    EditText phnNum=null, veri = null;

    FirebaseAuth au;
    Button forgotpass, login;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks otp;

    String verifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.userlogin);

        login = findViewById(R.id.loginButton);
        phnNum = findViewById(R.id.enter_phone);
        forgotpass = findViewById(R.id.forgot_pass);

        au = FirebaseAuth.getInstance();

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

                    if ((phnNum.getText().toString()).equals("")) {

                    (Toast.makeText(getApplicationContext(), "Please enter the phone number and proceed to receive an OTP", Toast.LENGTH_SHORT)).show();
                    }


                     else{ 
                         otp = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                            @Override
                            public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                            }

                            @Override
                            public void onVerificationFailed(@NonNull FirebaseException e) {
                            }

                            @Override
                            public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                                super.onCodeSent(s, forceResendingToken);

                                verifyCode = s;
                                (Toast.makeText(getApplicationContext(), "The OTP Code has been send, please verify the code", Toast.LENGTH_SHORT)).show();


                            }
                         };


                     }
                 }

        });
    }

            public void send_sms (View v){

                String i = (phnNum.getText()).toString();
                PhoneAuthProvider.getInstance().verifyPhoneNumber(i, 60, TimeUnit.SECONDS, this, otp);

                login.setOnClickListener(new View.OnClickListener() {
                    @Override

                    public void onClick(View view) {
                        Intent u = new Intent(view.getContext(), otp_verify.class);
                        startActivity(u);
                    }
                });

            }



            //SignIn Method
            // We will pass value in the method with "PhoneAuthCredential" data-type.
            public void SignIn(PhoneAuthCredential credential) {

                //" au " is the firebase variable and call the method

                    au.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful()) {

                            (Toast.makeText(getApplicationContext(), "You have Sign-In Successfully", Toast.LENGTH_SHORT)).show();
                            }
                        else{
                            (Toast.makeText(getApplicationContext(), "Please try again", Toast.LENGTH_SHORT)).show();
                        }
                    }
                });
            }
    }

当我使用空白 EditText 登录时,if 部分会执行,但是当我输入电话号码时,它不会执行 else 部分。我希望当用户输入他们的电话号码时 else 部分应该执行。

标签: javaandroidandroid-intent

解决方案


final String i = (phnNum.getText()).toString();

if ("".equals(i)) {

    (Toast.makeText(getApplicationContext(), "Please enter the phone number and proceed to receive an OTP", Toast.LENGTH_SHORT)).show();
} else { 

    // 1. prepare callback for async call
     otp = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
        }

        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);

            verifyCode = s;
            (Toast.makeText(getApplicationContext(), "The OTP Code has been send, please verify the code", Toast.LENGTH_SHORT)).show();

            Intent u = new Intent(userLogin.this, otp_verify.class);
            startActivity(u);
        }
     };


    // 2. execute actual call
    PhoneAuthProvider.getInstance().verifyPhoneNumber(i, 60, TimeUnit.SECONDS, userLogin.this, otp);

 }

代码片段准备回调并在 Firebase 身份验证调用中使用它。当验证码实际发送时,被onCodeSent调用的和新的活动启动。


推荐阅读