如何向firebase数据库添加用户名和密码

时间:2016-03-15 11:46:17

标签: android firebase firebase-authentication

如何获取用户名和密码EditText值并将其添加到Firebase数据库?

OnTouchListener用于我创建的名为REG的注册按钮。

请在MotionEvent.ACTION_UP:section。

中输入正确的代码
public class RegisterActivity extends AppCompatActivity implements View.OnTouchListener{
EditText USER_NAME, USER_PASS, CON_PASS;
LinearLayout REG;
Context ctx = this;
Firebase firebase;

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

    Firebase.setAndroidContext(this);
    firebase = new Firebase("https://burning-fire-2423.firebaseIO.com/");

    USER_NAME = (EditText) findViewById(R.id.reg_user);
    USER_PASS = (EditText) findViewById(R.id.reg_pass);
    CON_PASS = (EditText) findViewById(R.id.con_pass);
    REG = (LinearLayout) findViewById(R.id.user_reg);
    REG.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event){
    View view = (View) v;
    switch (view.getId()) {
        case R.id.user_reg:
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    firebase.createUser("example@hotmail.com", "password", new Firebase.ValueResultHandler<Map<String, Object>>() {
                        @Override
                        public void onSuccess(Map<String, Object> result) {
                            System.out.println("Successfully created user account with uid: " + result.get("uid"));
                        }
                        @Override
                        public void onError(FirebaseError firebaseError) {
                            // there was an error
                        }
                    });
                    break;
                case MotionEvent.ACTION_CANCEL:
                    break;
            }
            break;
    }
    return false;
}

}

1 个答案:

答案 0 :(得分:1)

我在这里创建了一个工作注册系统。

public class RegisterActivity extends AppCompatActivity implements    View.OnTouchListener{
EditText USER_NAME, USER_PASS, CON_PASS;
LinearLayout REG;
Context ctx = this;
Firebase firebase;

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

    Firebase.setAndroidContext(this);
    firebase = new Firebase("https://burning-fire-1234.firebaseIO.com/");

    USER_NAME = (EditText) findViewById(R.id.reg_user);
    USER_PASS = (EditText) findViewById(R.id.reg_pass);
    CON_PASS = (EditText) findViewById(R.id.con_pass);
    REG = (LinearLayout) findViewById(R.id.user_reg);
    REG.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event){
    View view = (View) v;
    switch (view.getId()) {
        case R.id.user_reg:
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    String email = USER_NAME.getText().toString();
                    String password = USER_PASS.getText().toString();
                    String confirm = CON_PASS.getText().toString();

                    if(password.equals(confirm))
                    {
                        if(password.length() >= 6)
                        {
                            firebase.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
                                @Override
                                public void onSuccess(Map<String, Object> result) {
                                    System.out.println("Successfully created user account with uid: " + result.get("uid"));
                                }

                                @Override
                                public void onError(FirebaseError firebaseError) {
                                    // there was an error
                                }
                            });
                        }
                        else
                        {
                            Toast.makeText(ctx, "Password must be at least 6 characters long", Toast.LENGTH_LONG).show();
                        }
                    }
                    else
                    {
                        USER_PASS.setText("");
                        CON_PASS.setText("");
                        Toast.makeText(ctx, "Passwords don't match", Toast.LENGTH_LONG).show();
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                    break;
            }
            break;
    }
    return false;
}

}