android - 我正在尝试使用 else if 语句来验证电子邮件,但是它会在 else if(password.isEmpty) 上抛出错误,说“else without if”
问题描述
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main);
Tmail = findViewById(R.id.login_email);
Tpass = findViewById(R.id.login_password);
Tconfirmpass = findViewById(R.id.loginRetype_password);
Bar = findViewById(R.id.bar);
button = findViewById(R.id.signin);
mAuth = FirebaseAuth.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email, password, confirm;
email = Tmail.getEditText().getText().toString().trim();
password = Tpass.getEditText().getText().toString().trim();
confirm = Tconfirmpass.getEditText().getText().toString().trim();
if (email.isEmpty()){
Tmail.getEditText().setError("Field should not be empty");
Tmail.requestFocus();
}
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches());{
Tmail.setError("Please enter a valid email Id");
Tmail.requestFocus();
}
else if (password.isEmpty()){
Tpass.setError("Please enter password");
Tpass.requestFocus();
} else if (password.length() < 6) {
Tpass.setError("Password too short");
Tpass.requestFocus();
} else if (!confirm.equals(password)) {
Tconfirmpass.setError("Password mismatch");
Tconfirmpass.requestFocus();
} else {mAuth.createUserWithEmailAndPassword(email, password)
.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
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
}
});
}
}
解决方案
请正确格式化您的代码。
删除该行中的分号,else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches());
您应该一切顺利。
推荐阅读
- c# - 如何从下拉列表中获取值并将其插入到 asp.net C# 中的表中
- google-chrome-extension - 如何防止 chrome 扩展在 chrome-urls 上执行
- android - http2 似乎不适用于 OkHttp3 和 retrofit2
- qt - 在 XP-Dev 中使用 QNetworkRequest
- sql - 如何在 GCP 数据库上访问 Magento bitnami 并运行 SQL 语句
- c++ - 如何避免原生可视化器中的递归?
- javascript - 添加一个类,然后仅在动画完成时将其删除
- android - 分页库使数据源无效
- nsis - 当我们使用 NSIS 运行安装程序时,如何在默认情况下在任务管理器中显示系统托盘图标以及相应的可执行文件?
- soapui - Soap UI - 对单个请求的多个响应