实现步骤图解:
判断输入框是否为空->密码两次输入是否相同->简介为空则是默认值->性别判断->注册signup,存入信息->成功
- 下面是注册功能的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:id="@+id/re_name"/>
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄"
android:id="@+id/re_age"/>
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="简介"
android:id="@+id/re_doc"/>
<RadioGroup
android:id="@+id/mRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"
android:id="@+id/re_boy"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="女"
android:id="@+id/re_girl"
/>
</RadioGroup>
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="numberPassword"
android:id="@+id/re_pass1"/>
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="再输入一次"
android:id="@+id/re_pass2"
android:inputType="numberPassword"/>
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="邮箱"
android:id="@+id/re_email"
/>
<Button
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"
android:text="注册"
android:layout_marginTop="10dp"
android:id="@+id/re_registered"/>
</LinearLayout>
效果图:
- 接下来是具体实现
- 首先是初始化控件
- 点击注册按钮后,获取用户输入的信息
- 使用TextUtils.isEmpty()判断是否为空,输入框不为空之后判断密码两次输入是否相同。(pass1.equals(pass2)),如果密码相同继续判断简介是否为空,简介为空则设置默认值
- 接下来获取性别,true为男,false为女
接下来存储信息,编写实体类继承BmobUser
通过set方法存入信息:
- signUp注册,成功失败都有Toast提示
RegisteredActivity源代码为:
public class RegisteredActivity extends BaseActivity implements View.OnClickListener {
private EditText et_name;
private EditText et_age;
private EditText et_doc;
private EditText et_email;
private EditText et_pass1;
private EditText et_pass2;
private RadioGroup mRadioGroup;
private RadioButton girl;
private RadioButton boy;
private boolean isGender;
private Button btn_registered;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registered);
initView();
}
private void initView() {
et_name=(EditText)findViewById(R.id.re_name);
et_age=(EditText)findViewById(R.id.re_age);
et_doc=(EditText)findViewById(R.id.re_doc);
et_email=(EditText)findViewById(R.id.re_email);
et_pass1=(EditText)findViewById(R.id.re_pass1);
et_pass2=(EditText)findViewById(R.id.re_pass2);
mRadioGroup=(RadioGroup) findViewById(R.id.mRadioGroup);
girl=(RadioButton)findViewById(R.id.re_girl);
boy=(RadioButton)findViewById(R.id.re_boy);
btn_registered=(Button)findViewById(R.id.re_registered);
btn_registered.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.re_registered:
//获取用户信息
String name=et_name.getText().toString().trim();
String age=et_age.getText().toString().trim();
String doc=et_doc.getText().toString().trim();
String email=et_email.getText().toString().trim();
String pass1=et_pass1.getText().toString().trim();
String pass2=et_pass2.getText().toString().trim();
//判断是否为空
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(age)&&!TextUtils.isEmpty(email)
&&!TextUtils.isEmpty(pass1)&&!TextUtils.isEmpty(pass2)){
//判断密码是否一致
if(pass1.equals(pass2)){
//判断doc的值,如果为空设置默认值
if(doc.isEmpty()){
doc="这个人很懒,什么也没有留下……";
}
//判断性别
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.re_boy){
isGender=true;
}else if(checkedId==R.id.re_girl){
isGender=false;
}
}
});
//存储用户信息
MyUSer myUSer=new MyUSer();
myUSer.setUsername(name);
myUSer.setAge(age);
myUSer.setEmail(email);
myUSer.setPassword(pass2);
myUSer.setDoc(doc);
myUSer.setGender(isGender);
//注册
myUSer.signUp(new SaveListener<MyUSer>() {
@Override
public void done(MyUSer myUSer, BmobException e) {
if(e==null){
Toast.makeText(RegisteredActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
}else{
Toast.makeText(RegisteredActivity.this, "注册失败"+e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}else{
Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
}
}
}
}