Android - Firebase - 电子邮件地址格式错误

时间:2017-09-10 09:26:17

标签: java android email firebase firebase-authentication

目标

允许用户使用<注册首选的显示名称家庭住址电子邮件密码 em> Firebase身份验证(电子邮件和密码)

Java类

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;

public class RegistrationActivity extends AppCompatActivity {

    private TextInputLayout jRegisterName;
    private TextInputLayout jRegisterAddress;
    private TextInputLayout jRegisterEmail;
    private TextInputLayout jRegisterPassword;
    private Button jRegisterRegBtn;

    //Firebase
    private FirebaseAuth mAuth;


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

        //Firebase Auth
        mAuth = FirebaseAuth.getInstance();

        jRegisterName = (TextInputLayout) findViewById(R.id.registerName);
        jRegisterAddress = (TextInputLayout) findViewById(R.id.registerAddress);
        jRegisterEmail = (TextInputLayout) findViewById(R.id.registerEmail);
        jRegisterPassword = (TextInputLayout) findViewById(R.id.registerPassword);
        jRegisterRegBtn = (Button) findViewById(R.id.registerRegBtn);

        jRegisterRegBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String userDisplayName = jRegisterName.getEditText().toString();
                String userHomeAddress = jRegisterAddress.getEditText().toString();
                String userEmail = jRegisterEmail.getEditText().toString().trim();
                String userPassword = jRegisterPassword.getEditText().toString().trim();

                registerUser(userDisplayName, userHomeAddress, userEmail, userPassword);

            }

            private void registerUser(String userDisplayName, String userHomeAddress, String userEmail, String userPassword) {
                mAuth.createUserWithEmailAndPassword(userEmail, userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    Intent intentMain = new Intent(RegistrationActivity.this, MainActivity.class);
                                    startActivity(intentMain);
                                    finish();
                                }else if(!task.isSuccessful()){
                                    FirebaseAuthException e = (FirebaseAuthException )task.getException();
                                    Toast.makeText(RegistrationActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        });
            }
        });

    }
}

问题

用户输入

等输入后

姓名:JohnSmith

HomeAddress:18 King Street

电子邮件:johnsmith@gmail.com

密码:123456789pass

用户无法注册,下面的消息提示

“注册失败:电子邮件地址格式错误”

2 个答案:

答案 0 :(得分:3)

检查xml文件中的inputType。电子邮件中有三种类型的输入。

android:inputType="textWebEmailAddress"
android:inputType="textEmailAddress"
android:inputType="textEmailSubject"

更多https://developer.android.com/reference/android/text/InputType.html

答案 1 :(得分:1)

通过这样做完成

  • 您需要setInputType
android:inputType="textEmailAddress"
  • 您可以使用programmatic
  • 完成
emailEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS

乐意帮助您