英文:
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_fname
、s_lname
和 s_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.
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("The Email is required");
s_email.requestFocus();
return;
}
if (Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
Toast.makeText(getApplicationContext(), "The Email is regietered",
Toast.LENGTH_SHORT).show();
}
if (password.length() < 6) {
s_password.setError("The password is less than 6 characters");
s_password.requestFocus();
return;
}
if (password.isEmpty()) {
s_password.setError("The password is required");
s_password.requestFocus();
return;
}
if (cpassword.isEmpty()) {
s_password.setError("The comfirm password is required");
s_password.requestFocus();
return;
}
if(!cpassword.equals(password)){
s_cpasserd.setError("The password is not much! ");
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(), "Error ! ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "The user has been registered ",
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<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Error ! ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "The user has been registered ",
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<String, Object> userInfo = new HashMap<>();
city.put("name", "name_here");
city.put("gender", "gender_here");
city.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);
}
});
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论