当我尝试验证一个空的editText(OTP)时,我“不幸的是应用程序已停止”,我想要做的就是试图在用户尝试验证一个空的editText(OTP)时举杯。
我试图在if语句中禁用按钮(如果mcodeText为空,则禁用按钮),但是,因为im使用带有按钮整数的单个按钮来获取OTP并验证它是否无效
public class LoginActivity extends AppCompatActivity {
private LinearLayout mLayout, nLayout;
private EditText mPhoneText, mCodeText;
private TextView mTextview, nTextview, kTexiview;
private Button mButton, mButton2;
private FirebaseAuth mAuth;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private int btnType = 0;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
mLayout = (LinearLayout) findViewById(R.id.PhoneOL);
nLayout = (LinearLayout) findViewById(R.id.socialL);
mTextview = (TextView) findViewById(R.id.textView17);
kTexiview = (TextView) findViewById(R.id.textView21);
kTexiview.setVisibility(View.GONE);
nTextview = (TextView) findViewById(R.id.TandC);
mCodeText = (EditText) findViewById(R.id.editText2);
mButton = (Button) findViewById(R.id.buttonP);
mPhoneText = (EditText) findViewById(R.id.editText);
mPhoneText.setText("+91-");
Selection.setSelection(mPhoneText.getText(), mPhoneText.getText().length());
mPhoneText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().startsWith("+91-")) {
mPhoneText.setText("+91-");
Selection.setSelection(mPhoneText.getText(), mPhoneText.getText().length());
}
}
});
mButton2 = (Button) findViewById(R.id.phone1);
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mButton2.setVisibility(View.GONE);
mLayout.setVisibility(View.VISIBLE);
mTextview.setVisibility(View.GONE);
nLayout.setVisibility(View.GONE);
nTextview.setVisibility(View.GONE);
kTexiview.setVisibility(View.VISIBLE);
mCodeText.setVisibility(View.GONE);
}
});
mButton = (Button) findViewById(R.id.buttonP);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (btnType == 0) {
String phoneNumber = mPhoneText.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, 60, TimeUnit.SECONDS,
LoginActivity.this, mCallbacks);
mCodeText.setVisibility(View.VISIBLE);
mPhoneText.setVisibility(View.GONE);
mButton.setText("Sing In");
} else {
String verificationCode = mCodeText.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, verificationCode);
signInWithPhoneAuthCredential(credential);
}
}
});
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(getApplicationContext(),
"Enter a Valid Number", Toast.LENGTH_SHORT).show();
mPhoneText.setVisibility(View.VISIBLE);
mPhoneText.setText("");
mCodeText.setVisibility(View.GONE);
mButton.setText("Proceed");
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
mResendToken = token;
btnType = 1;
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener < AuthResult > () {
@Override
public void onComplete(@NonNull Task < AuthResult > task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = task.getResult().getUser();
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(getApplicationContext(),
"Logged in Successfully", Toast.LENGTH_SHORT).show();
} else {
// Sign in failed, display a message and update the UI
Toast.makeText(getApplicationContext(),
"Your entered wrong OTP, Try again",
Toast.LENGTH_SHORT).show();
}
}
});
}
}