实现正则验证手机的登录与注册

导入依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.bigkoo:convenientbanner:2.0.5'
compile 'org.xutils:xutils:3.5.0'
compile 'com.google.code.gson:gson:2.8.2'

添加权限
网络权限和注册

android:name=".MyApplication"

//
MainActivty页面
package com.mrli.rk01;

import android.content.Intent;
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.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_login;
    private Button btn_login_rejister;
    private EditText login_phone;
    private EditText login_pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setonclick();
        btn_login_rejister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2=new Intent(MainActivity.this,Rejist.class);
                startActivity(intent2);
            }
        });

    }

    private void setonclick() {
        btn_login.setOnClickListener(this);
        btn_login_rejister.setOnClickListener(this);
    }

    private void init() {
        btn_login = (Button) findViewById(R.id.login);
        btn_login_rejister = (Button) findViewById(R.id.login_rejister);
        login_phone = (EditText) findViewById(R.id.login_phone);
        login_pwd = (EditText) findViewById(R.id.login_pwd);
    }


    @Override
    public void onClick(View view) {
        String mobile = login_phone.getText().toString().trim();
        String password = login_pwd.getText().toString().trim();
        switch (view.getId()){
            case R.id.login:
//                Intent intent3=new Intent(MainActivity.this,Login.class);
//                startActivity(intent3);
                boolean flag = checkData(mobile, password);
                //Log.d("zzz", "checkData: "+flag);
                if(flag){
                    register(mobile,password);
                }
                break;
            case R.id.login_rejister:
                Intent intent2=new Intent(MainActivity.this,Rejist.class);
                startActivity(intent2);
                break;
        }
    }
    private void register(String mobile, String password) {
        RequestParams params = new RequestParams("http://120.27.23.105/user/reg");
        //Log.d("zzz", "params: "+params);
        params.addQueryStringParameter("mobile",mobile);
        params.addQueryStringParameter("password",password);

        x.http().get(params, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                RegistBean registBean = gson.fromJson(result, RegistBean.class);
                Intent intent = new Intent(MainActivity.this, Login.class);
                startActivity(intent);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }
    private boolean checkData(String mobile, String password) {
        if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(password)){
            Toast.makeText(this,"不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(password.length()<6){
            Toast.makeText(this,"密码长度不能小于6",Toast.LENGTH_SHORT).show();
            return false;
        }
       return true;
    }
}
 
/
注册页面
package com.mrli.rk01;

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.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class Rejist extends AppCompatActivity implements View.OnClickListener {

    private EditText rejist_phone;
    private EditText rejist_pwd;
    private Button rejist_rejister;

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

        init();
        setonclick();



    }

    private void setonclick() {
        rejist_rejister.setOnClickListener(this);
    }


    private void init() {
        rejist_phone = (EditText) findViewById(R.id.rejist_phone);
        rejist_pwd = (EditText) findViewById(R.id.rejist_pwd);
        rejist_rejister = (Button) findViewById(R.id.rejist_rejister);
    }

    @Override
    public void onClick(View view) {
        String mobile = rejist_phone.getText().toString().trim();
        String password = rejist_pwd.getText().toString().trim();

        switch (view.getId()){
            case R.id.rejist_rejister:
                boolean flag = checkData(mobile, password);
                //Log.d("zzz", "checkData: "+flag);
                if(flag){
                    register(mobile,password);
                }
                break;
        }
    }

    private void register(String mobile, String password) {
        RequestParams params = new RequestParams("http://120.27.23.105/user/reg");
        //Log.d("zzz", "params: "+params);
        params.addQueryStringParameter("mobile",mobile);
        params.addQueryStringParameter("password",password);

        x.http().get(params, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                RegistBean registBean = gson.fromJson(result, RegistBean.class);
                Toast.makeText(Rejist.this,registBean.getMsg(),Toast.LENGTH_SHORT).show();
                //Log.d("zzz", "Msg: "+registBean.getMsg());
                if(registBean.getCode().equals("0")){
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }

    private boolean checkData(String mobile, String password) {
        if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(password)){
            Toast.makeText(this,"不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(password.length()<6){
            Toast.makeText(this,"密码长度不能小于6",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
bean包
package com.mrli.rk01;

/**
 * {"msg":"天呢!用户名或密码不能为空","code":"1","data":"{}"}.
 */

class RegistBean {
    private String msg;
    private String code;
    private String data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
/
需要导入Application并进行注册
package com.mrli.rk01;

import android.app.Application;

import org.xutils.x;

/**
 * Created by SDC on 2017/10/31.
 */

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
    }
}
布局文件
main  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mrli.rk01.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:gravity="center"
        android:textSize="18dp"
        android:text="登录"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="vertical"
            android:id="@+id/shuru">

            <EditText
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:id="@+id/login_phone"
                android:hint="请输入手机号"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_marginTop="15dp" />

            <EditText
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:id="@+id/login_pwd"
                android:gravity="center"
                android:layout_gravity="center"
                android:hint="请输入密码"
                android:layout_marginTop="13dp"
                 />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_below="@id/shuru"
            android:gravity="center_horizontal"
            android:orientation="horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/login"
                android:text="登录"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/login_rejister"
                android:text="注册"/>
        </LinearLayout>


</RelativeLayout>


注册页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mrli.rk01.Rejist">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="vertical"
        android:id="@+id/shuru">

        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/rejist_phone"
            android:hint="请输入手机号"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_marginTop="15dp" />

        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/rejist_pwd"
            android:gravity="center"
            android:layout_gravity="center"
            android:hint="请输入密码"
            android:layout_marginTop="13dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_below="@id/shuru"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rejist_rejister"
            android:text="注册"/>
    </LinearLayout>

</RelativeLayout>


注册页面
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值