如何在Firebase身份验证中添加用户名?

时间:2017-04-02 21:13:04

标签: android firebase firebase-authentication

我找到了一些快速提示,例如从电子邮件中获取用户名,但我希望用户输入自己的用户名,然后使用它来关注其他用户名。我该怎么做?

到目前为止我的注册活动:

private EditText etUsername;
private EditText etEmail;
private EditText etPassword;
private Button bRegister;
private TextView tvSignin;

private ProgressDialog progressDialog;
private DatabaseReference mDatabase;
private FirebaseAuth firebaseAuth;

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

    progressDialog = new ProgressDialog(this);

    mDatabase = FirebaseDatabase.getInstance().getReference();
    firebaseAuth = FirebaseAuth.getInstance();

    etUsername = (EditText) findViewById(R.id.etUsername);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPassword = (EditText) findViewById(R.id.etPassword);

    bRegister = (Button) findViewById(R.id.bRegister);
    tvSignin = (TextView) findViewById(R.id.tvSignIn);

    bRegister.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            registerUser();
        }
    });

    tvSignin.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
        }
    });
}


private void registerUser()
{
    String username = etUsername.getText().toString().trim();
    String email = etEmail.getText().toString().trim();
    String password = etPassword.getText().toString().trim();

    if(username.isEmpty() || email.isEmpty() || password.isEmpty())
    {
        Toast.makeText(this, "Fill in the empty fields", Toast.LENGTH_SHORT).show();
        return;
    }

    progressDialog.setMessage("Registering user");
    progressDialog.show();

    firebaseAuth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
            {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                    if(task.isSuccessful())
                    {
                        Toast.makeText(RegisterActivity.this, "Successfully registered", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(RegisterActivity.this, "Error occurred while registering, please try again", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    progressDialog.hide();
}

0 个答案:

没有答案