登录页面---MVP---记住密码,自动登录

MainActivity

package wanghuiqi.bawie.com.app1;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import wanghuiqi.bawie.com.app1.presenter.UserPresenter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mTel;
    private EditText mPwd;
    private Button mSubmit;
    private ProgressDialog mProgressDialog;
    private UserPresenter userPresenter;
    private SharedPreferences mPref;
    private SharedPreferences.Editor mEdit;
    private String tel;
    private String pwd;
    private CheckBox mRemPwd;
    private CheckBox mAutoLogin;
    private String userTel;
    private String userPwd;

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

        //创建pref对象
        mPref = PreferenceManager.getDefaultSharedPreferences(this);
        mEdit = mPref.edit();

        pwdAndAuto();
    }

    private void pwdAndAuto() {
        //是否记住密码
        boolean isRememberPwd = mPref.getBoolean("isRememberPwd", false);
        if(isRememberPwd){
            userTel = mPref.getString("userTel", "");
            userPwd = mPref.getString("userPwd", "");
            mTel.setText(userTel);
            mPwd.setText(userPwd);
            mRemPwd.setChecked(true);
        }
        //是否自动登录
        boolean isRememberAuto = mPref.getBoolean("isRememberAuto", false);
        if(isRememberAuto){
            startActivity(new Intent(MainActivity.this,MyPageActivity.class));
        }
        mAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mRemPwd.setChecked(true);
                }else{
                    mRemPwd.setChecked(false);
                }
            }
        });

    }

    private void initView() {
        mTel = findViewById(R.id.tel);
        mPwd = findViewById(R.id.pwd);
        mSubmit = findViewById(R.id.submit);
        mRemPwd = findViewById(R.id.rem_pwd);
        mAutoLogin = findViewById(R.id.auto_login);
        //点击登录
        mSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit:
                click();
                break;
        }
    }

    private String str = "http://www.xieast.com/api/user/login.php?username=13800138000&password=123456";

    @SuppressLint("StaticFieldLeak")
    private void click() {

        //获取输入框的内容
        tel = mTel.getText().toString().trim();
        pwd = mPwd.getText().toString().trim();
        //1、非空判断
        userPresenter = new UserPresenter();
        boolean empty = userPresenter.isEmpty(tel, pwd);
        if(!empty){
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        //2、手机格式
        final boolean mobileNO = userPresenter.isMobileNO(tel);
        //判断手机格式
        if (!mobileNO){
            Toast.makeText(MainActivity.this, "手机号格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }



        //3、从P层调用model的网络请求数据
        userPresenter.netWorkTelAndPwd(str);

        userPresenter.setUserInterface(new UserPresenter.UserInterface() {
            @Override
            public void success() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mRemPwd.isChecked()){
                            mEdit.putString("userTel",tel);
                            mEdit.putString("userPwd",pwd);
                            mEdit.putBoolean("isRememberPwd",true);
                            mEdit.commit();
                        }
                        if(mAutoLogin.isChecked()){
                            mEdit.putBoolean("isRememberAuto",true);
                            mEdit.commit();
                        }
                        startActivity(new Intent(MainActivity.this,MyPageActivity.class));
                    }
                });

            }

            @Override
            public void failed() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}

Bean层

User–输入框

private String telephone;
private String password;

MessageBean—网络获取

package wanghuiqi.bawie.com.app1.model;

public class MessageBean {

    /**
     * msg : 登录成功
     * code : 100
     * data : {"id":1,"name":"admin","mobile":13800138000}
     */

    private String msg;
    private int code;
    private DataBean data;

    public String getMsg() {
        return msg;
    }

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

    public int getCode() {
        return code;
    }

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

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * id : 1
         * name : admin
         * mobile : 13800138000
         */

        private int id;
        private String name;
        private String mobile;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }
    }
}

UserPresenter—P层

package wanghuiqi.bawie.com.app1.presenter;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;

import wanghuiqi.bawie.com.app1.MainActivity;
import wanghuiqi.bawie.com.app1.model.HttpUtils;
import wanghuiqi.bawie.com.app1.model.MessageBean;
import wanghuiqi.bawie.com.app1.model.User;

public class UserPresenter {

    private User mUser;

    public boolean isEmpty(String tel, String pwd){
        mUser = new User(tel,pwd);
        if (TextUtils.isEmpty(tel)||TextUtils.isEmpty(pwd)){
            return false;
        }else{
            return true;
        }
    }

    //验证手机号
    public static boolean isMobileNO(String mobileNums) {
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(16[0-9])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }



    //网络并请求的手机号和密码
    public void netWorkTelAndPwd(String str){

        HttpUtils httpUtils = new HttpUtils();
        httpUtils.newWorkUser(str);
        httpUtils.setNetWorkInterface(new HttpUtils.NetWorkInterface() {
            @Override
            public void stringResult(MessageBean.DataBean dataBean) {
                if(dataBean.getMobile().equals(mUser.getTelephone()) && dataBean.getName().equals(mUser.getPassword())){
                    mUserInterface.success();
                }else{
                    mUserInterface.failed();
                }
            }
        });

    }
    public interface UserInterface{
        void success();
        void failed();
    }
    private UserInterface mUserInterface;
    public void setUserInterface(UserInterface userInterface){
        mUserInterface=userInterface;
    }
}

HttpUtils工具层

package wanghuiqi.bawie.com.app1.model;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtils {

    public void newWorkUser(final String strUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(strUrl);

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    if (connection.getResponseCode() == 200) {
                        InputStream is = connection.getInputStream();
                        String result = getFromInputStream(is);
                        MessageBean messageBean = new Gson().fromJson(result, MessageBean.class);
                        MessageBean.DataBean data = messageBean.getData();


                        mNetWorkInterface.stringResult(data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private String getFromInputStream(InputStream is) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        try {
            for (String tmp = br.readLine(); tmp != null; tmp = br.readLine()) {
                sb.append(tmp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    //接口
    public interface NetWorkInterface{
        void stringResult(MessageBean.DataBean dataBean);
    }
    private NetWorkInterface mNetWorkInterface;
    public void setNetWorkInterface(NetWorkInterface netWorkInterface){
        mNetWorkInterface=netWorkInterface;
    }
}

权限

activity_main

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机号"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/tel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密     码"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/pwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/rem_pwd"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="记住密码" />
        <CheckBox
            android:id="@+id/auto_login"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="自动登录" />
    </LinearLayout>
    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"
        android:background="@drawable/sub"/>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值