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) { /* 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通) 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */ String num = "[1][358]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有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; } }