登录注册

LoginActivity
package com.bwie.zhangjun.view.activity;

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.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.zhangjun.R;
import com.bwie.zhangjun.model.bean.LoginBean;
import com.bwie.zhangjun.presenter.LoginPesenter;
import com.bwie.zhangjun.utils.Constant;
import com.bwie.zhangjun.utils.VerifyUtil;
import com.bwie.zhangjun.view.iview.LoginIView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.drawee_view)
    SimpleDraweeView drawee_view;
    @BindView(R.id.text_username)
    EditText text_username;
    @BindView(R.id.text_password)
    EditText text_password;
    @BindView(R.id.text_newname)
    TextView text_newname;
    @BindView(R.id.bt_login)
    Button bt_login;
    @BindView(R.id.linear)
    LinearLayout linear;
    @BindView(R.id.simpleDraweeView)
    SimpleDraweeView simpleDraweeView;
    @BindView(R.id.qq_login)
    TextView qq_login;
    private LoginPesenter loginPesenter;
    private String mobile;
    private String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        loginPesenter = new LoginPesenter();
    }

    @OnClick({R.id.text_newname,R.id.bt_login, R.id.qq_login})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.text_newname:
                final Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_login:
                mobile = text_username.getText().toString().trim();
                password = text_password.getText().toString().trim();
                String pwd = md5(password);
                loginPesenter.getLoginData(Constant.LOGIN_URL, mobile, password);
                loginPesenter.attachView(new LoginIView() {
                    @Override
                    public void onLoginSuccess(LoginBean loginBean) {
                        boolean tel= VerifyUtil.isMobile(mobile);
                        if (mobile.isEmpty()){
                            Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                        }else {
                            if (!tel) {
                                Toast.makeText(LoginActivity.this, "用户名格式不对", Toast.LENGTH_SHORT).show();
                            } else {
                                if (password.isEmpty()) {
                                    Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                                } else {
                                    if (password.length() < 6) {
                                        Toast.makeText(LoginActivity.this, "不能小于6", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Intent intent1 = new Intent(LoginActivity.this,HomeActivity.class);
                                        intent1.putExtra("uid",loginBean.getData().getUid());
                                        startActivity(intent1);
                                    }
                                }
                            }
                        }
                    }
                });
                break;
            case R.id.qq_login:
                break;
        }
    }
    public static String md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loginPesenter!=null){
            loginPesenter.dattachView();
        }
    }
}

登录的布局

<?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"
    android:padding="10dp"
    tools:context="com.bwie.zhangjun.view.activity.LoginActivity">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/drawee_view"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        app:placeholderImage="@mipmap/ic_launcher_round" />

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/drawee_view"
        android:orientation="vertical">

        <EditText
            android:id="@+id/text_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="手机号/会员号/邮箱" />

        <EditText
            android:id="@+id/text_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:password="true"
            android:hint="请输入密码" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="忘记密码" />

            <TextView
                android:id="@+id/text_newname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="300dp"
                android:text="新用户注册" />
        </LinearLayout>

        <Button
            android:id="@+id/bt_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/beijing"
            android:text="登录" />
    </LinearLayout>

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/simpleDraweeView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_below="@+id/linear"
        android:layout_centerInParent="true"
        android:layout_marginTop="20dp"
        app:placeholderImage="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/qq_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/simpleDraweeView"
        android:layout_centerHorizontal="true"
        android:text="QQ登录" />
</RelativeLayout>

注册

RegActivity

package com.bwie.zhangjun.view.activity;

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

import com.bwie.zhangjun.R;
import com.bwie.zhangjun.model.bean.RegBean;
import com.bwie.zhangjun.presenter.RegPesenter;
import com.bwie.zhangjun.utils.Constant;
import com.bwie.zhangjun.utils.VerifyUtil;
import com.bwie.zhangjun.view.iview.RegIView;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegActivity extends AppCompatActivity {

    @BindView(R.id.text_username)
    EditText text_username;
    @BindView(R.id.text_password)
    EditText text_password;
    @BindView(R.id.text_againpassword)
    EditText text_againpassword;
    @BindView(R.id.text_youxiang)
    EditText text_youxiang;
    @BindView(R.id.bt_reg)
    Button bt_reg;
    @BindView(R.id.drawee_view)
    SimpleDraweeView drawee_view;
    private RegPesenter regPesenter;
    private String mobile;
    private String password;
    private String password1;
    private String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        ButterKnife.bind(this);
        regPesenter = new RegPesenter();
    }

    @OnClick({R.id.bt_reg, R.id.drawee_view})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt_reg:
                mobile = text_username.getText().toString().trim();
                password = text_password.getText().toString().trim();
                password1 = text_againpassword.getText().toString().trim();
                email = text_youxiang.getText().toString().trim();
                regPesenter.getRegData(Constant.REG_URL, mobile, password);
                regPesenter.attachView(new RegIView() {
                    @Override
                    public void onRegSuccess(RegBean regBean) {
                        boolean tel= VerifyUtil.isMobile(mobile);
                        if (mobile.isEmpty()){
                            Toast.makeText(RegActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                        }else {
                            if (!tel) {
                                Toast.makeText(RegActivity.this, "用户名格式不对", Toast.LENGTH_SHORT).show();
                            } else {
                                if (password.isEmpty()) {
                                    Toast.makeText(RegActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                                } else {
                                    if (password.length() < 6) {
                                        Toast.makeText(RegActivity.this, "不能小于6", Toast.LENGTH_SHORT).show();
                                    } else {
                                        if (password.equals(password1)) {
                                            if (email.isEmpty()) {
                                                Toast.makeText(RegActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
                                            } else {
                                                Intent intent1 = new Intent(RegActivity.this, HomeActivity.class);
                                                startActivity(intent1);
                                                finish();
                                            }
                                        } else {
                                            Toast.makeText(RegActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
                break;
            case R.id.drawee_view:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (regPesenter!=null){
            regPesenter.dattachView();
        }
    }
}


M

package com.bwie.zhangjun.model;

import com.bwie.zhangjun.model.bean.RegBean;
import com.bwie.zhangjun.presenter.inter.RegPesenterInter;
import com.bwie.zhangjun.utils.RetrofitUrils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * Created by lenovo on 2018/4/29.
 */

public class RegModel {
    private final RegPesenterInter regPesenterInter;

    public RegModel(RegPesenterInter regPesenterInter) {
        this.regPesenterInter=regPesenterInter;
    }
    public void getRegData(String regUrl,String mobile,String password){
        Map<String, String> params=new HashMap<>();
        params.put("mobile",mobile);
        params.put("password",password);
        RetrofitUrils.getService1().doGet(regUrl,params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            String string = responseBody.string();
                            RegBean regBean = new Gson().fromJson(string, RegBean.class);
                            regPesenterInter.onRegSuccess(regBean);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}

P

package com.bwie.zhangjun.presenter;

import com.bwie.zhangjun.model.RegModel;
import com.bwie.zhangjun.model.bean.RegBean;
import com.bwie.zhangjun.presenter.inter.RegPesenterInter;
import com.bwie.zhangjun.view.iview.RegIView;

/**
 * Created by lenovo on 2018/4/29.
 */

public class RegPesenter implements RegPesenterInter {

    private RegModel regModel;
    private RegIView regIView;
    public void attachView(RegIView regIView) {
        this.regIView = regIView;
    }

    public void dattachView() {
        if (regIView != null) {
            regIView = null;
        }
    }
    public RegPesenter() {
        regModel = new RegModel(this);
    }
    public void getRegData(String regUrl,String mobile,String password){
        regModel.getRegData(regUrl,mobile,password);
    }
    @Override
    public void onRegSuccess(RegBean regBean) {
        regIView.onRegSuccess(regBean);
    }
}

package com.bwie.zhangjun.utils;

import android.text.TextUtils;

/**
 * Created by lenovo on 2018/4/29.
 */

public class VerifyUtil {
    /**
     * 验证手机格式
     */
    public static boolean isMobile(String number) {
    /*
    移动:134135136137138139150151157(TD)158159187188
    联通:130131132152155156185186
    电信:133153180189、(1349卫通)
    总结起来就是第一位必定为1,第二位必定为358,其他位置的可以为0-9
    */
        String num = "[1][358]\\d{9}";//"[1]"代表第1位为数字1"[358]"代表第二位可以为358中的一个,"\\d{9}"代表后面是可以是09的数字,有9位。
        if (TextUtils.isEmpty(number)) {
            return false;
        } else {
            //matches():字符串是否在给定的正则表达式匹配
            return number.matches(num);
        }
    }
    public static boolean verifyLength(String password){
        if(password.length() < 6){
            return false;
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值