Android Studio / Firebase电子邮件注册

时间:2016-10-21 07:22:34

标签: android email firebase

我目前有登录/注册设置,用户必须先登记才能登录。在我的register.xml我有4个EditText' s rusername,{{ 1}},remailrpassword)和提交按钮rconfirmPassword

registerb

这是java:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:layout_marginLeft="80dp"
        android:layout_marginStart="80dp"
        android:id="@+id/rusername"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColorHint="#000"
        android:textColor="#000"
        android:hint="Username" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:id="@+id/remail"
        android:textColorHint="#000"
        android:textColor="#000"
        android:layout_marginLeft="80dp"
        android:layout_below="@+id/rusername"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:hint="Email" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:id="@+id/rpassword"
        android:textColorHint="#000"
        android:textColor="#000"
        android:layout_marginLeft="80dp"
        android:layout_below="@+id/remail"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:hint="Password" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:id="@+id/rpasswordconfirm"
        android:textColorHint="#000"
        android:textColor="#000"
        android:layout_marginLeft="80dp"
        android:layout_below="@+id/rpassword"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:hint="Password" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/registerb"
        android:layout_below="@+id/rpasswordconfirm"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp" />

</RelativeLayout>
  • 问题:

当用户输入虚假电子邮件时,Firebase会显示,只有当我输入电子邮件时,例如public class Register extends AppCompatActivity { EditText musername; EditText memail; EditText mpassword; EditText mconfirmpass; Button mregister; private FirebaseAuth mAuth; private ProgressDialog mProgress; private DatabaseReference mDatabse; int minL; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); musername = (EditText) findViewById(R.id.rusername); memail = (EditText) findViewById(R.id.remail); mpassword = (EditText) findViewById(R.id.rpassword); mconfirmpass = (EditText) findViewById(R.id.rpasswordconfirm); mregister = (Button) findViewById(R.id.registerb); minL = 5; mDatabse = FirebaseDatabase.getInstance().getReference().child("Users"); mProgress = new ProgressDialog(this); mAuth = FirebaseAuth.getInstance(); mregister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startRegister(); } }); } private void startRegister() { String character = "@"; final String name = musername.getText().toString().trim(); String mail = memail.getText().toString().trim(); String pass = mpassword.getText().toString().trim(); String passconf = mconfirmpass.getText().toString().trim(); if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf) && pass.equals(passconf) && pass.length() > 5) { mProgress.setMessage("Signing up..."); mProgress.show(); mAuth.createUserWithEmailAndPassword(mail, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { String user_id = mAuth.getCurrentUser().getUid(); DatabaseReference current_user_db = mDatabse.child(user_id); current_user_db.child("username").setValue(name); current_user_db.child("image").setValue("default"); mProgress.dismiss(); Intent intent = new Intent(Register.this, Home.class); // Clear other activities intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); finish(); } } }); }else { if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf) && pass.equals(passconf) && pass.length() < 6){ Toast.makeText(Register.this, "Password should be 6 or more", Toast.LENGTH_SHORT).show(); } if (TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please enter Username", Toast.LENGTH_SHORT).show(); }if (TextUtils.isEmpty(name) && TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please enter your information", Toast.LENGTH_SHORT).show(); }if (!TextUtils.isEmpty(name) && TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please enter your email", Toast.LENGTH_SHORT).show(); }if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please enter your password", Toast.LENGTH_SHORT).show(); }if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please confirm your password", Toast.LENGTH_SHORT).show(); }if (!TextUtils.isEmpty(name) && TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please enter Email", Toast.LENGTH_SHORT).show(); }if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf)){ Toast.makeText(Register.this, "Please enter Password", Toast.LENGTH_SHORT).show(); } if (!pass.equals(passconf)){ Toast.makeText(Register.this, "Please check password combination", Toast.LENGTH_SHORT).show(); } }} } 才会注册成功。如果电子邮件不正确,我的###@###.com将继续加载。

  • 问题:

如何在几秒钟后解除progressDialog并显示progressDialog,告诉用户输入有效的电子邮件?

  • 编辑:

我发布了解决问题的方法,请参阅下面的答案。

3 个答案:

答案 0 :(得分:0)

public class Register extends AppCompatActivity {
EditText musername;
EditText memail;
EditText mpassword;
EditText mconfirmpass;
Button mregister;
private FirebaseAuth mAuth;
private ProgressDialog mProgress;
private DatabaseReference mDatabse;
int minL;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    musername = (EditText) findViewById(R.id.rusername);
    memail = (EditText) findViewById(R.id.remail);
    mpassword = (EditText) findViewById(R.id.rpassword);
    mconfirmpass = (EditText) findViewById(R.id.rpasswordconfirm);
    mregister = (Button) findViewById(R.id.registerb);
    minL = 5;

    mDatabse = FirebaseDatabase.getInstance().getReference().child("Users");

    mProgress = new ProgressDialog(this);

    mAuth = FirebaseAuth.getInstance();

    mregister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        if(musername.getText().toString().trim().length() <=0)
        { 
        Toast.makeText(Register.this, "Please enter Username", Toast.LENGTH_SHORT).show();
        }else if(memail.getText().toString().trim().length() <=0 )
        { 
        Toast.makeText(Register.this, "Please enter email", Toast.LENGTH_SHORT).show();
        }else if(!isValidEmail(memail.getText().toString()))
        { 
        Toast.makeText(Register.this, "Please enter valid email", Toast.LENGTH_SHORT).show();
        }else if(mpassword.getText().toString().trim().length() <=0 &&  mpassword.getText().toString().trim().length() >6)
        { 
        Toast.makeText(Register.this, "Please enter vlid password", Toast.LENGTH_SHORT).show();
        }else if(mconfirmpass.getText().toString().trim().length() <=0  &&  mpassword.getText().toString().trim().length >6)
        { 
        Toast.makeText(Register.this, "Please enter valid conf password", Toast.LENGTH_SHORT).show();
        }else{

            startRegister();
        }
        }
    });


}
public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}

private void startRegister() {

    String character = "@";
    final String name = musername.getText().toString().trim();
    String mail = memail.getText().toString().trim();
    String pass = mpassword.getText().toString().trim();
    String passconf = mconfirmpass.getText().toString().trim();

        mProgress.setMessage("Signing up...");
        mProgress.show();
        mAuth.createUserWithEmailAndPassword(mail, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {

                    String user_id = mAuth.getCurrentUser().getUid();
                    DatabaseReference current_user_db = mDatabse.child(user_id);
                    current_user_db.child("username").setValue(name);
                    current_user_db.child("image").setValue("default");

                    mProgress.dismiss();

                    Intent intent = new Intent(Register.this, Home.class);
                    // Clear other activities
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(intent);
                    finish();


                }

            }
        });

    }
}

答案 1 :(得分:0)

我只是检查你检查的代码

if (task.isSuccessful()) {
//your code
}
else{
mProgress.dismiss();
}

试试这个。

答案 2 :(得分:0)

这对我有用,谢谢你的贡献。

public class Register extends AppCompatActivity {
EditText musername;
EditText memail;
EditText mpassword;
EditText mconfirmpass;
Button mregister;
private FirebaseAuth mAuth;
private ProgressDialog mProgress;
private DatabaseReference mDatabse;
int minL;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    musername = (EditText) findViewById(R.id.rusername);
    memail = (EditText) findViewById(R.id.remail);
    mpassword = (EditText) findViewById(R.id.rpassword);
    mconfirmpass = (EditText) findViewById(R.id.rpasswordconfirm);
    mregister = (Button) findViewById(R.id.registerb);
    minL = 5;

    mDatabse = FirebaseDatabase.getInstance().getReference().child("Users");
    mProgress = new ProgressDialog(this);
    mAuth = FirebaseAuth.getInstance();

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

            startRegister();
        }
    });

}


private void startRegister() {

    String character = "@";
    final String name = musername.getText().toString().trim();
    String mail = memail.getText().toString().trim();
    String pass = mpassword.getText().toString().trim();
    String passconf = mconfirmpass.getText().toString().trim();

    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf) && pass.equals(passconf) && pass.length() > 5 && isValidEmail(memail.getText().toString())) {

        mProgress.setMessage("Signing up...");
        mProgress.show();
        mAuth.createUserWithEmailAndPassword(mail, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {

                    String user_id = mAuth.getCurrentUser().getUid();
                    DatabaseReference current_user_db = mDatabse.child(user_id);
                    current_user_db.child("username").setValue(name);
                    current_user_db.child("image").setValue("default");

                    mProgress.dismiss();

                    Intent intent = new Intent(Register.this, Home.class);
                    // Clear other activities
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(intent);
                    finish();


                }

            }
        });

    } else if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf) && pass.equals(passconf) && pass.length() < 6) {

        Toast.makeText(Register.this, "Password should be 6 or more", Toast.LENGTH_SHORT).show();

    } else if (!isValidEmail(memail.getText().toString()) && !TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf) && pass.equals(passconf) && pass.length() > 5) {
        Toast.makeText(Register.this, "Please enter valid email", Toast.LENGTH_SHORT).show();
    } else if (TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please enter Username", Toast.LENGTH_SHORT).show();

    } else if (TextUtils.isEmpty(name) && TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please enter your information", Toast.LENGTH_SHORT).show();

    } else if (!TextUtils.isEmpty(name) && TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please enter your email", Toast.LENGTH_SHORT).show();

    } else if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please enter your password", Toast.LENGTH_SHORT).show();

    } else if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please confirm your password", Toast.LENGTH_SHORT).show();

    } else if (!TextUtils.isEmpty(name) && TextUtils.isEmpty(mail) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please enter Email", Toast.LENGTH_SHORT).show();

    } else if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(mail) && TextUtils.isEmpty(pass) && !TextUtils.isEmpty(passconf)) {

        Toast.makeText(Register.this, "Please enter Password", Toast.LENGTH_SHORT).show();

    } else if (!pass.equals(passconf)) {

        Toast.makeText(Register.this, "Please check password combination", Toast.LENGTH_SHORT).show();

    }

}

public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}

}