如何在Android Studio中使用Firebase创建包括名字、姓氏等信息的注册表单。

huangapple go评论97阅读模式
英文:

how can do signup form include fname last name etc on firebase using android studio

问题

以下是代码的中文翻译部分:

package com.example.showapps;

import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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;

public class signupActivity extends AppCompatActivity implements View.OnClickListener {

    EditText s_email, s_password, s_cpasserd, fname, lname, phone;
    ProgressBar progressBar;
    FirebaseAuth firebaseAuth;

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

        s_email = findViewById(R.id.s_email);
        s_password = findViewById(R.id.s_password);
        s_cpasserd = findViewById(R.id.s_cpassword);
        lname = findViewById(R.id.s_lname);
        fname = findViewById(R.id.s_fname);
        phone = findViewById(R.id.s_phone);

        firebaseAuth = FirebaseAuth.getInstance();
        progressBar = findViewById(R.id.progressBar2);
        findViewById(R.id.btn_signup).setOnClickListener(this);
        findViewById(R.id.mTologin).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_signup:
                signup();
                break;
            case R.id.mTologin:
                startActivity(new Intent(getApplicationContext(), login_Activity.class));
                break;
        }
    }

    private void signup() {
        String email = s_email.getText().toString().trim();
        String password = s_password.getText().toString().trim();
        String cpassword = s_cpasserd.getText().toString().trim();
        String fname = s_fname.getText().toString().trim();
        String lname = s_lname.getText().toString().trim();
        String phone = s_phone.getText().toString().trim();

        if (email.isEmpty()) {
            s_email.setError("需要填写电子邮件");
            s_email.requestFocus();
            return;
        }
        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            Toast.makeText(getApplicationContext(), "电子邮件格式不正确", Toast.LENGTH_SHORT).show();
        }
        if (password.length() < 6) {
            s_password.setError("密码长度不能少于6个字符");
            s_password.requestFocus();
            return;
        }
        if (password.isEmpty()) {
            s_password.setError("需要填写密码");
            s_password.requestFocus();
            return;
        }
        if (cpassword.isEmpty()) {
            s_cpasserd.setError("需要填写确认密码");
            s_cpasserd.requestFocus();
            return;
        }

        if (!cpassword.equals(password)) {
            s_cpasserd.setError("密码不匹配");
            s_cpasserd.requestFocus();
            return;
        }

        progressBar.setVisibility(View.VISIBLE);

        firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new
            OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                progressBar.setVisibility(View.GONE);
                if (!task.isSuccessful()) {
                    Toast.makeText(getApplicationContext(), "注册出错", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "用户已注册", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplicationContext(), login_Activity.class));
                }
            }
        });
    }
}

请注意,您在代码中引用了 s_fnames_lnames_phone 这些 EditText,但是在代码中并没有找到对应的初始化。您需要确保在 onCreate 方法中为这些 EditText 初始化,类似于其他 EditText 的初始化方式。

英文:

this code for signup form on firebase using an android studio and it's working fine but I need to add first name, last name, gender with the signup form, and record to a firebase user group and direct go to a specific group

This picture shows what I want and I do it manually.

如何在Android Studio中使用Firebase创建包括名字、姓氏等信息的注册表单。

package com.example.showapps;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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;
public class signupActivity extends AppCompatActivity implements View.OnClickListener {
EditText s_email, s_password, s_cpasserd , fname,lname,phone;
ProgressBar progressBar;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
s_email = findViewById(R.id.s_email);
s_password = findViewById(R.id.s_password);
s_cpasserd = findViewById(R.id.s_cpassword);
lname= findViewById(R.id.s_lname);
fname = findViewById(R.id.s_fname);
phone= findViewById(R.id.s_phone);
firebaseAuth = FirebaseAuth.getInstance();
progressBar = findViewById(R.id.progressBar2);
findViewById(R.id.btn_signup).setOnClickListener(this);
findViewById(R.id.mTologin).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_signup:
signup();
break;
case R.id.mTologin:
startActivity(new Intent(getApplicationContext(), login_Activity.class));
break;
}
}
private void signup() {
String email = s_email.getText().toString().trim();
String password = s_password.getText().toString().trim();
String cpassword = s_cpasserd.getText().toString().trim();
String fname= s_fname.getText().toString().trim();
String lname = s_lname.getText().toString().trim();
String phone = s_phone.getText().toString().trim();
if (email.isEmpty()) {
s_email.setError(&quot;The Email is required&quot;);
s_email.requestFocus();
return;
}
if (Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
Toast.makeText(getApplicationContext(), &quot;The Email is regietered&quot;, 
Toast.LENGTH_SHORT).show();
}
if (password.length() &lt; 6) {
s_password.setError(&quot;The password is less than 6 characters&quot;);
s_password.requestFocus();
return;
}
if (password.isEmpty()) {
s_password.setError(&quot;The password is required&quot;);
s_password.requestFocus();
return;
}
if (cpassword.isEmpty()) {
s_password.setError(&quot;The comfirm password is required&quot;);
s_password.requestFocus();
return;
}
if(!cpassword.equals(password)){
s_cpasserd.setError(&quot;The password is not much! &quot;);
s_cpasserd.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new 
OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), &quot;Error ! &quot;, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), &quot;The user has been registered &quot;, 
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), login_Activity.class));
}
}
});
}
}

here what i need to add for this code to complete my task ?

firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new 
OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), &quot;Error ! &quot;, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), &quot;The user has been registered &quot;, 
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), login_Activity.class));
}
}
});

Thanks

答案1

得分: 1

else 语句块内部,您需要将数据发送到 Firestore:

} else {
    Map<String, Object> userInfo = new HashMap<>();
    userInfo.put("name", "name_here");
    userInfo.put("gender", "gender_here");
    userInfo.put("email", email);
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("customer_user").document("1")
        .set(userInfo)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "DocumentSnapshot successfully written!");
                Toast.makeText(getApplicationContext(), "The user has been registered",
                        Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getApplicationContext(), login_Activity.class));
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Error writing document", e);
            }
        });
}

创建一个 hashmap,然后使用 set 方法将数据添加到文档中,并在操作成功时导航到其他活动。

英文:

Inside the else statement, you need to send the data to firestore:

 } else {
Map&lt;String, Object&gt; userInfo = new HashMap&lt;&gt;();
city.put(&quot;name&quot;, &quot;name_here&quot;);
city.put(&quot;gender&quot;, &quot;gender_here&quot;);
city.put(&quot;email&quot;, email);
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection(&quot;customer _user&quot;).document(&quot;1&quot;)
.set(userInfo)
.addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, &quot;DocumentSnapshot successfully written!&quot;);
Toast.makeText(getApplicationContext(), &quot;The user has been registered &quot;, 
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), login_Activity.class));
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, &quot;Error writing document&quot;, e);
}
});
}

Create a hashmap then using set you can add the data to the document and navigate to the other activity in case it was successful.

huangapple
  • 本文由 发表于 2020年7月26日 16:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定