//依賴 compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'org.kie.modules:com-google-code-gson:6.5.0.Final'
compile 'com.google.code.gson:gson:2.8.2'
登入注冊布局和工具
//
drawable下xml
left_item_font_selector.xml;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorRed" android:state_pressed="true"></item>
<item android:color="@color/colorBlack" android:state_pressed="false"></item>
</selector>
//left_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorDark" android:state_pressed="true"></item>
<item android:drawable="@color/colorWhite" android:state_pressed="false"></item>
</selector>
//values下的colors
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorRed">#ff3660</color>
<color name="colorDark">#EEE</color>
<color name="colorWhite">#FFFFFF</color>
<color name="colorBlack">#FF262426</color>
</resources>
//登入布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".view.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="京東登陸" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="賬号" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:hint="手機号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="密碼" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入密碼" />
<ImageView
android:id="@+id/iv_pwd"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:background="@drawable/yannormal" />
</LinearLayout>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="登陸" />
<!--<TextView-->
<!--android:id="@+id/tv_register"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginLeft="15dp"-->
<!--android:text="請注冊" />-->
<Button
android:id="@+id/bt_register"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="注冊" />
</LinearLayout>
//注冊布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".view.RegisterActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="京東注冊" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="賬号" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:hint="手機号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="密碼" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入密碼" />
<ImageView
android:id="@+id/iv_pwd"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:background="@drawable/yannormal" />
</LinearLayout>
<Button
android:id="@+id/bt_zhuce"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="注冊" />
</LinearLayout>
//BaseActivity view包下的類
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityStroreUtil.addActivity(this);
}
}
//net包下的ActivityStroreUtil
public class ActivityStroreUtil {
private static List<Activity> list=new LinkedList<>();
public static void addActivity(Activity ac){
list.add(ac);
}
/**
* 關閉已經開啟的Activity
*/
public void finishAll(){
for (Activity ac:list){
if(ac != null){
ac.finish();
}
}
}
}
//view包下內建BaseModel
public class BaseModel {
protected Handler handler=
new Handler(Looper.getMainLooper());
}
net包
HttpUtils類
public class HttpUtils {
private static volatile HttpUtils httpUtils;
private final OkHttpClient client;
private Context context;
private HttpUtils(){
//1.建立OkHttpClient
client = new OkHttpClient();
this.context=context;
}
public static HttpUtils getHttpUtils(){
if(httpUtils == null){
synchronized (HttpUtils.class){
if(httpUtils==null){
httpUtils=new HttpUtils();
}
}
}
return httpUtils;
}
/**
* Get請求
*/
public void doGet(String url, Callback callback){
//判斷是否有網
if (NetworkUtils.isAvailable(context)){
//網絡不可用
Toast.makeText(context, "網絡不可用", Toast.LENGTH_SHORT).show();
return;
}
//建立一個Request
Request request=new Request.Builder().url(url).build();
//發送請求
client.newCall(request).enqueue(callback);
}
/**
* POST請求
*
* @param url
* @param params
* @param callback
*/
public void doPost(String url, Map<String,String> params,Callback callback){
//這裡可以加網絡判斷
//判斷參數
if(params == null || params.size() == 0){
throw new RuntimeException("params is null!!!");
}
//建立一個請求
FormBody.Builder builder=new FormBody.Builder();
for(Map.Entry<String,String> entry:params.entrySet()){
builder.add(entry.getKey(),entry.getValue());
}
FormBody formBody=builder.build();
Request request=new Request.Builder().url(url).post(formBody).build();
//發送請求
client.newCall(request).enqueue(callback);
}
}
OnNetListenter類
public interface OnNetListenter<T> {
//成功回調
public void OnSuccess(T t);
//失敗回調
public void OnFailure(Exception e);
}
Api類
public class Api {
public static boolean isOnline=false;
public static final String DEV="http://120.27.23.105/";
public static final String WORK="";
public static final String HOST=isOnline ? WORK : DEV;
public static final String LOGIN=HOST+"user/login";//登陸
public static final String REGISTER=HOST+"user/reg";//注冊;
}
model層登入注冊都要繼承的類
public class BaseModel {
protected Handler handler=
new Handler(Looper.getMainLooper());
}
model層接口登陸
public interface ILoginModel {
//Get請求
//Post請求
public void login(String account, String pwd, OnNetListenter<LoginBean> onNetListenter);
}
model層登入接口
public class LoginModel extends BaseModel implements ILoginModel {
@Override
public void login(String account, String pwd, final OnNetListenter<LoginBean> onNetListenter) {
Map<String,String> params=new HashMap<>();
params.put("mobile",account);
params.put("password",pwd);
HttpUtils.getHttpUtils().doPost(Api.LOGIN, params, new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListenter.OnFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string=response.body().string();
final LoginBean loginBean=new Gson().fromJson(string,LoginBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListenter.OnSuccess(loginBean);
}
});
}
});
}
}
//model層注冊
public interface IRegisterModel {
public void register(String account, String pwd, OnNetListenter<BaseBean> onNetListenter);
}
//model層注冊接口
public class RegisterModel extends BaseModel implements IRegisterModel {
@Override
public void register(String account, String pwd, final OnNetListenter<BaseBean> onNetListenter) {
Map<String,String> params=new HashMap<>();
params.put("mobile",account);
params.put("password",pwd);
HttpUtils.getHttpUtils().doPost(Api.REGISTER, params, new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListenter.OnFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final BaseBean baseBean= new Gson().fromJson(string,BaseBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListenter.OnSuccess(baseBean);
}
});
}
});
}
}
//登入presenter層
public class MainPresenter {
private IMainActivity iMainActivity;
private final ILoginModel iLoginModel;
public MainPresenter(IMainActivity iMainActivity) {
this.iMainActivity=iMainActivity;
iLoginModel=new LoginModel();
}
public void login(){
String account=iMainActivity.getAccount();
String pwd=iMainActivity.getPwd();
//判斷賬号密碼是否正确
if (checkAccount(account) && checkPwd(pwd)) {
//去調用model,進行登陸
iLoginModel.login(account, pwd, new OnNetListenter<LoginBean>() {
@Override
public void OnSuccess(LoginBean loginBean) {
//儲存登陸成功後的資料,可以儲存到SP,也可以儲存到資料庫
iMainActivity.show(loginBean.getMsg());
//跳轉到分類界面
iMainActivity.toClassAc();
}
@Override
public void OnFailure(Exception e) {
}
});
}
}
private boolean checkPwd(String pwd) {
if(TextUtils.isEmpty(pwd)){
//給使用者提示,輸入的賬号不能為空
iMainActivity.show("請輸入的賬号不能為空");
return false;
}
if(pwd.length() !=6){
iMainActivity.show("請輸入6位數密碼");
return false;
}
return true;
}
/**
* 驗證手機号是否正确
*
* @param account
*/
private boolean checkAccount(String account) {
if (TextUtils.isEmpty(account)){
//給使用者提示,輸入的賬号不能為空
iMainActivity.show("請輸入賬号");
return false;
}
if (!isMobileNO(account)) {
iMainActivity.show("請輸入正确的手機号");
return false;
}
return true;
}
/*
判斷是否是手機号
*/
public static boolean isMobileNO(String mobiles) {
Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}
public void register() {
//其實就是跳轉到注冊頁面
iMainActivity.toRegisterAc();
}
}
//presenter層注冊
public class RegisterPresenter {
private IRegisterActivity iRegisterActivity;
private IRegisterModel iRegisterModel;
public RegisterPresenter(IRegisterActivity iRegisterActivity) {
this.iRegisterActivity=iRegisterActivity;
iRegisterModel=new RegisterModel();
}
private boolean checkPwd(String pwd) {
if (TextUtils.isEmpty(pwd)) {
//給使用者提示,輸入的賬号不能為空
iRegisterActivity.show("請輸入密碼");
return false;
}
if (pwd.length() != 6) {
iRegisterActivity.show("請輸入6位密碼");
return false;
}
return true;
}
/**
* 驗證手機号是否正确
*
* @param account
*/
private boolean checkAccount(String account) {
if (TextUtils.isEmpty(account)) {
//給使用者提示,輸入的賬号不能為空
iRegisterActivity.show("請輸入賬号");
return false;
}
if (!isMobileNO(account)) {
iRegisterActivity.show("請輸入正确的手機号");
return false;
}
return true;
}
/*
判斷是否是手機号
*/
public static boolean isMobileNO(String mobiles) {
Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}
//注冊
public void register(){
String account=iRegisterActivity.getAccount();
String pwd = iRegisterActivity.getPwd();
if(checkAccount(account) && checkPwd(pwd)){
iRegisterModel.register(account, pwd, new OnNetListenter<BaseBean>() {
@Override
public void OnSuccess(BaseBean baseBean) {
//成功以後,回到登陸界面
if(baseBean.getCode().equals("1")){
iRegisterActivity.show(baseBean.getMsg());
}else{
iRegisterActivity.show(baseBean.getMsg());
//跳轉頁面時銷毀本界面
iRegisterActivity.finishAc();
}
}
@Override
public void OnFailure(Exception e) {
}
});
}
}
}
//登入view層接口
public interface IMainActivity {
public String getAccount();
public String getPwd();
public void show(String str);
public void toRegisterAc();
}
//注冊view層接口
public interface IRegisterActivity {
public String getAccount();
public String getPwd();
public void finishAc();
public void show(String str);
public void toRegisterAc();
//這個是登陸成功跳轉頁面的方法
public void toClassAc();
}
//登入view層繼承接口
public class MainActivity extends BaseActivity implements IMainActivity, View.OnClickListener {
private ImageView mBackIv;
private EditText mPhoneEt;
private EditText mPwdEt;
private ImageView mPwdIv;
private Button mLoginBt;
private Button bt_register;
private MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//關聯presenter
mainPresenter = new MainPresenter(this);
initView();
}
private void initView() {
mBackIv = (ImageView) findViewById(R.id.iv_back);
mPhoneEt = (EditText) findViewById(R.id.et_phone);
mPwdEt = (EditText) findViewById(R.id.et_pwd);
mPwdIv = (ImageView) findViewById(R.id.iv_pwd);
mLoginBt = (Button) findViewById(R.id.bt_login);
mLoginBt.setOnClickListener(this);
bt_register = (Button) findViewById(R.id.bt_register);
bt_register.setOnClickListener(this);
}
//重寫登入成功後跳轉的方法
@Override
public void toClassAc() {
Intent intent = new Intent(MainActivity.this, ClassActivity.class);
startActivity(intent);
}
@Override
public String getAccount() {
return mPhoneEt.getText().toString().trim();
}
@Override
public String getPwd() {
return mPwdEt.getText().toString().trim();
}
@Override
public void show(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
//跳轉頁面的方法
@Override
public void toRegisterAc() {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
mainPresenter.login();
break;
case R.id.bt_register:
// 注冊
mainPresenter.register();
break;
}
}
}
//注冊view層繼承接口
public class RegisterActivity extends AppCompatActivity implements IRegisterActivity, View.OnClickListener {
private ImageView mBackIv;
private EditText mPhoneEt;
private EditText mPwdEt;
private ImageView mPwdIv;
private Button mLoginBt;
private RegisterPresenter registerPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
registerPresenter = new RegisterPresenter(this);
initView();
}
private void initView() {
mBackIv = (ImageView) findViewById(R.id.iv_back);
mPhoneEt = (EditText) findViewById(R.id.et_phone);
mPwdEt = (EditText) findViewById(R.id.et_pwd);
mPwdIv = (ImageView) findViewById(R.id.iv_pwd);
mLoginBt = (Button) findViewById(R.id.bt_zhuce);
mLoginBt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_zhuce:
//這個按鈕是注冊
registerPresenter.register();
break;
default:
break;
}
}
@Override
public String getAccount() {
return mPhoneEt.getText().toString().trim();
}
@Override
public String getPwd() {
return mPwdEt.getText().toString().trim();
}
//銷毀本頁面
@Override
public void finishAc() {
finish();
}
@Override
public void show(String str) {
Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
}
}