昨天发表的用户注册用户的方法有问题,忽略了密码这个问题,才发现所谓的手机号快捷登陆时有一定的缺陷的,官方的Api中给了得方法不能直接修改原密码,需要一定的验证码,因为有所冲突,所以我觉得还是用用户名和密码来进行注册用户,比较简单而且方便,就是感觉不是很严谨。。。下次再研究研究,其实最好的还是自己写后台,下个月起开始自己学写后台,加油。
下面看一下,我改过的用户注册功能的代码:
下面是register_activity.xml的代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f8f8f8" android:orientation="vertical" tools:context="com.elainetaylor.taoyi.ui.activity.LoginActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="horizontal"> <ImageButton android:id="@+id/ib_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@mipmap/icon_back" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginEnd="50dp" android:layout_marginRight="50dp" android:gravity="center" android:text="注册" android:textColor="#707070" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#dadada" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" android:textColor="#707070" android:textSize="16sp" /> <EditText android:id="@+id/et_username" android:layout_width="245dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:background="@android:color/transparent" android:inputType="text" android:textColor="#5000" android:textSize="14sp" /> </LinearLayout> <View android:layout_width="300dp" android:layout_height="1dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="#3000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textColor="#707070" android:textSize="16sp" /> <EditText android:id="@+id/et_password" android:layout_width="260dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:background="@android:color/transparent" android:inputType="textPassword" android:textColor="#5000" android:textSize="14sp" /> </LinearLayout> <View android:layout_width="300dp" android:layout_height="1dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="#3000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重复密码" android:textColor="#707070" android:textSize="16sp" /> <EditText android:id="@+id/et_rePassword" android:layout_width="230dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:background="@android:color/transparent" android:inputType="textPassword" android:textColor="#5000" android:textSize="14sp" /> </LinearLayout> <View android:layout_width="300dp" android:layout_height="1dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="#3000" /> </LinearLayout> <Button android:id="@+id/btn_register" android:layout_width="300dp" android:layout_height="35dp" android:layout_gravity="center" android:layout_marginTop="30dp" android:background="#00b7ee" android:gravity="center" android:text="完成注册" android:textColor="#fff" android:textSize="15sp" /> </LinearLayout> </LinearLayout>下面是RegisterActivity.class的代码:
package com.elainetaylor.taoyi.ui.activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; import com.avos.avoscloud.AVException; import com.avos.avoscloud.AVOSCloud; import com.avos.avoscloud.AVUser; import com.avos.avoscloud.SignUpCallback; import com.elainetaylor.taoyi.R; public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { private EditText etUsername, etPassword, etRePassword; private Button btnRegister; private ImageButton btnBack; private String username, password, rePassword; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); AVOSCloud.initialize(this, "K1Ki1nEU9GFmUK6lfVMBMeHE-9Nh9j0Va", "pwYculXduYevPHtGDk6LuI0c"); init(); } public void init() { etUsername = (EditText) findViewById(R.id.et_username); etPassword = (EditText) findViewById(R.id.et_password); etRePassword = (EditText) findViewById(R.id.et_rePassword); btnRegister = (Button) findViewById(R.id.btn_register); btnBack = (ImageButton) findViewById(R.id.ib_back); btnRegister.setOnClickListener(this); btnBack.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.ib_back: Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); finish(); break; case R.id.btn_register: username = etUsername.getText().toString(); password = etPassword.getText().toString(); rePassword = etRePassword.getText().toString(); if (username.isEmpty()) { Toast.makeText(RegisterActivity.this, "亲,请输入用户名哦", Toast.LENGTH_SHORT).show(); } else if (password.isEmpty()) { Toast.makeText(RegisterActivity.this, "亲,请输入密码哦", Toast.LENGTH_SHORT).show(); } else if (rePassword.isEmpty()) { Toast.makeText(RegisterActivity.this, "亲,请重复输入密码哦", Toast.LENGTH_SHORT).show(); } else if (!password.equals(rePassword)) { Toast.makeText(RegisterActivity.this, "亲,请输入相同的密码哦", Toast.LENGTH_SHORT).show(); etRePassword.setText(""); } else if (password.length() < 6) { Toast.makeText(RegisterActivity.this, "亲,请输入超过六位的密码哦", Toast.LENGTH_SHORT).show(); } else { showProgressDialog("注册中..."); makeRegister(username, password); } break; } } public void makeRegister(String username, String password) { AVUser user = new AVUser(); user.setUsername(username); user.setPassword(password); user.signUpInBackground(new SignUpCallback() { @Override public void done(AVException e) { if (e == null) { missProgressDialog(); Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); finish(); } else if (e.getCode() == 202) { missProgressDialog(); Toast.makeText(RegisterActivity.this, "用户名已经被占用", Toast.LENGTH_SHORT).show(); } else { missProgressDialog(); Toast.makeText(RegisterActivity.this, "亲,网络有点问题", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }); } public void showProgressDialog(String msg) { if (null == dialog) { dialog = new ProgressDialog(this); dialog.setCanceledOnTouchOutside(false); } dialog.setMessage(msg); dialog.show(); } public void missProgressDialog() { if (null != dialog) { dialog.dismiss(); } } }加入了一个交互效果,progressbar,还是比较有点意思的,在这记录一下。