安卓项目之淘忆2.0的代码实现之注册功能

不知道为啥,刚开始总出错,现在找到了,应该是书写的方法有问题,现在找到了。。把之前验证的那个方法变通了一下,但是我不知对于后面的登陆会不会有冲突,所以现在也先这么写着,等后面有问题的时候在解决吧。

下面是regist_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:orientation="vertical"
    tools:context="com.elainetaylor.taoyi.ui.activity.LoginActivity"
    android:background="#f8f8f8">

    <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:textSize="16sp"
                android:textColor="#707070"/>

            <EditText
                android:id="@+id/et_phone"
                android:layout_width="260dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:background="@android:color/transparent"
                android:inputType="phone"
                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:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="短信验证码"
                android:textSize="16sp"
                android:textColor="#707070"/>

            <EditText
                android:id="@+id/et_CheckMode"
                android:layout_width="125sp"
                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" />

            <Button
                android:id="@+id/btn_checkMode"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:background="#00b7ee"
                android:text="获取验证码"
                android:textColor="#fff"
                android:textSize="13sp" />
        </LinearLayout>

        <View
            android:layout_width="300dp"
            android:layout_height="1dp"
            android:layout_gravity="center"
            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:textSize="16sp"
                android:textColor="#707070"/>

            <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>

    <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>
xml文件中,我没有做什么动画处理或者是其他的特效,下面看一下RegisterActivity.class的内容如下:

package com.elainetaylor.taoyi.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
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.LogInCallback;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.elainetaylor.taoyi.R;
import com.elainetaylor.taoyi.util.TimeCountUtil;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etPhone, etCheckMode, etPassword;
    private Button btnCheckMode, btnRegister;
    private ImageButton btnBack;
    private String phone, checkMode, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        AVOSCloud.initialize(this, "K1Ki1nEU9GFmUK6lfVMBMeHE-9Nh9j0Va", "pwYculXduYevPHtGDk6LuI0c");
        init();
    }

    public void init() {
        etPhone = (EditText) findViewById(R.id.et_phone);
        etCheckMode = (EditText) findViewById(R.id.et_CheckMode);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnCheckMode = (Button) findViewById(R.id.btn_checkMode);
        btnRegister = (Button) findViewById(R.id.btn_register);
        btnBack = (ImageButton) findViewById(R.id.ib_back);
        btnCheckMode.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        btnBack.setOnClickListener(this);
    }


    public static boolean isPhoneNum(String phone) {
        String tel = "[1][34578]\\d{9}";
        if (TextUtils.isEmpty(phone)) {
            return false;
        } else {
            return phone.matches(tel);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.btn_checkMode:
                phone = etPhone.getText().toString();
                if (phone.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "亲,请输入手机号哦", Toast.LENGTH_SHORT).show();
                } else if (!isPhoneNum(phone)) {
                    Toast.makeText(RegisterActivity.this, "亲,请输入正确的手机号哦", Toast.LENGTH_SHORT).show();
                } else {
                    getCheckMode(phone);
                }
                break;
            case R.id.btn_register:
                phone = etPhone.getText().toString();
                checkMode = etCheckMode.getText().toString();
                password = etPassword.getText().toString();
                if (phone.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "亲,请输入手机号哦", Toast.LENGTH_SHORT).show();
                } else if (!isPhoneNum(phone)) {
                    Toast.makeText(RegisterActivity.this, "亲,请输入正确的手机号哦", Toast.LENGTH_SHORT).show();
                } else if (checkMode.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "亲,请输入验证码哦", Toast.LENGTH_SHORT).show();
                } else if (password.isEmpty() || password.length() < 6) {
                    Toast.makeText(RegisterActivity.this, "亲,请输入超过六位的秘密哦", Toast.LENGTH_SHORT).show();
                } else {
                    isCheckModeR(phone, checkMode);
                }
                break;
        }
    }

    public void getCheckMode(String phone) {
        AVOSCloud.requestSMSCodeInBackground(phone, new RequestMobileCodeCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(RegisterActivity.this, "获取验证码中", Toast.LENGTH_SHORT).show();
                    changeBtnCheckModeStyle();
                } else {
                    e.printStackTrace();
                }
            }
        });
    }

    public void changeBtnCheckModeStyle() {
        TimeCountUtil timeCountUtil = new TimeCountUtil(this, 60000, 1000, btnCheckMode);
        timeCountUtil.start();
    }

    public void isCheckModeR(String phone, String checkMode) {
        AVUser.signUpOrLoginByMobilePhoneInBackground(phone, checkMode, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (e == null) {
                    Toast.makeText(RegisterActivity.this, "亲,注册成功", Toast.LENGTH_SHORT).show();
                } else if (e.getCode() == 214) {
                    Toast.makeText(RegisterActivity.this, "亲,该手机号已经注册,请登录", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(RegisterActivity.this, "亲,您的网络有点问题", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
之前我也说过,我用的是leanCloud的后台,所以调用了一下相关的API。基本的注册功能已经实现,但是总觉得还有点小缺陷,不知道在哪里。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值