简单的登录注册

废话不多说  直接上代码   MVP借口回掉模式

MianActivity

public class MainActivity extends AppCompatActivity implements LoginContract.LoginView {
    @BindView(R.id.btn_reg)
    TextView btnReg;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.pass)
    EditText pass;
    @BindView(R.id.btn_log)
    Button btnLog;
    private LoginPresenterImpl presenter;
    private SharedPreferences mSharedPreferences;
    private CheckBox rem;
    private String ipass;
    private String iphone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mSharedPreferences = getSharedPreferences("lcj",MODE_PRIVATE);
        rem = findViewById(R.id.login_jizhu);
        //监听记住密码和跳转页面
        bindEvent();
        //记住密码
        rememberThePassword();
        //***************  1.登录
        //获取P曾对象
        presenter = new LoginPresenterImpl();

        presenter.attachView(this);

        btnLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户名、密码
                 iphone = phone.getText().toString();
                 ipass = pass.getText().toString();
                if (TextUtils.isEmpty(iphone) && TextUtils.isEmpty(ipass)) {
                    Toast.makeText(MainActivity.this, "电话号码/密码不能为空", Toast.LENGTH_LONG).show();
                }
                //获取P层,触发请求动作
                presenter.requestLoginData(iphone, ipass);
            }
        });

        //************* ** 2.注册
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });
    }


    //记住复选框密码和跳转注册页面
    private void bindEvent() {
        rem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mSharedPreferences.edit().putBoolean("rememberPassword",true).commit();
                }else {
                    mSharedPreferences.edit().putBoolean("rememberPassword",false).commit();
                }
            }
        });
    }

    //记住密码
    private void rememberThePassword() {
        //判断记住密码复选框是否选中
        if (mSharedPreferences.getBoolean("rememberPassword",false)){
            rem.setChecked(true);
            phone.setText(mSharedPreferences.getString("phone",""));
            pass.setText(mSharedPreferences.getString("pass",""));
        }
    }

    @Override
    public void showData(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
                if (TextUtils.isEmpty(iphone) && TextUtils.isEmpty(ipass)) {
                    Toast.makeText(MainActivity.this, "电话号码/密码不能为空", Toast.LENGTH_LONG).show();

                }else {
                    Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView(this);
    }
}

RegActivity//注册

public class RegActivity extends AppCompatActivity implements RegContract.RegView {
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.pass)
    EditText pass;
    @BindView(R.id.reg)
    Button reg;
    @BindView(R.id.reg_yanzheng)
    EditText regYanzheng;
    @BindView(R.id.reg_login)
    TextView regLogin;
    private RegPresenterImpl Presenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        ButterKnife.bind(this);
        Presenter = new RegPresenterImpl();
        Presenter.attachView(this);
        regLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.reg)
    public void onViewClicked() {
        //获取用户名、密码
        String iphone = phone.getText().toString();
        String ipass = pass.getText().toString();
        Log.e("lcj", "aaa");
        Presenter.requestData(iphone, ipass);
    }

    @Override
    public void showdata(String response) {
        Toast.makeText(this, response, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Presenter.detachView(this);
    }
}

契约类接口

public interface LoginContract {
    //V层
    public interface LoginView{
        public void showData(String response);
    }
    //P层
    public interface  LoginPresenter{
        //绑定
        public void attachView(LoginContract.LoginView loginView);
        //解绑
        public void detachView(LoginContract.LoginView loginView);
        //请求处理
        public void requestLoginData(String phone,String pass);
    }
    //M层
    public interface LoginModel{
        //请求处理
        public void containLoginResponseData(String phone,String pass,CallBack callBack);
        public interface CallBack{
            //回显数据
            public void responseData(String response);
        }
    }
}

注册契约类接口

public interface RegContract {
    //V层
    public interface RegView{
        public void showdata(String response);
    }
    //P层
    public interface RegPresenter {
        //接收V层对象
        public void attachView(RegView regView);
        //解绑
        public void detachView(RegView regView);
        //交给M层进行数据请求的处理----注册
        public void requestData(String phone, String pass);
    }
    //M层
    public interface RegModel {
        //接收数据接口  并且返回数据
        public void containData(String phone,String pass,CallBack callBack);
        //定义接口
        public interface CallBack{
            public void responseData(String response);
        }
    }
}

LoginModel登录

public class LoginModelImpl implements LoginContract.LoginModel {
    //登录的URL
    public static final String LOGIN_URL = "http://172.17.8.100/small/user/v1/login";
    @Override
    public void containLoginResponseData(String phone, String pass, CallBack callBack) {
        //TODO 请求数据
        new NowAysncTask(phone,pass,callBack).execute(LOGIN_URL);
    }

    private class NowAysncTask extends AsyncTask<String,Integer,String> {
        String phone;
        String pass;
        CallBack callBack;
        public NowAysncTask(String phone, String pass,CallBack callBack) {
            this.phone = phone;
            this.pass = pass;
            this.callBack = callBack;
        }
        @Override
        protected String doInBackground(String... urlStrings) {
            try {
                //请求的URL字串
                String urlString = urlStrings[0];
                //String completeUrlString = urlString + "?phone=\'PhoneNumber\'+pwd=\'password\'";
                String completeUrlString = urlString + "?phone="+phone+"&pwd="+pass;
                //请求的URL地址
                URL url = new URL(completeUrlString);
                //请求对象
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //设置请求方法
                //参数提交
                //提交的数据有PhoneName,password
                connection.setRequestMethod("POST");
                //连接
                connection.connect();
                Log.i("aaaa",connection.getResponseCode()+"");
                //判断请求码
                if (connection.getResponseCode() == 200) {
                    StringBuilder stringBuilder = new StringBuilder();
                    String line = null;
                    //输入流读取
                    InputStream inputStream = connection.getInputStream();
                    //高效字节流
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                    while ((line = br.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    //把读取的内容转换成字串
                    return stringBuilder.toString();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);
            //数据回传给P层,接口毁掉
            Log.e("NowAysncTask",response);
            callBack.responseData(response);
        }
    }
}

RegModel

public class RegModelImpl implements RegContract.RegModel {

    private static final String REQUEST_URL ="http://172.17.8.100/small/user/v1/register";

    @Override
    public void containData(String phone, String pass, CallBack callBack) {
        //TODO 请求数据
        new NowAysncTask(phone,pass,callBack).execute(REQUEST_URL);
    }

    private class NowAysncTask extends AsyncTask<String,Integer,String> {
        String phone;
        String pass;
        CallBack callBack;
        public NowAysncTask(String phone, String pass,CallBack callBack) {
            this.phone = phone;
            this.pass = pass;
            this.callBack = callBack;
        }

        @Override
        protected String doInBackground(String... urlStrings) {
            try {
                //请求的URL字串
                String urlString = urlStrings[0];
                //String completeUrlString = urlString + "?phone=\'PhoneNumber\'+pwd=\'password\'";
                String completeUrlString = urlString + "?phone="+phone+"&pwd="+pass;
                //请求的URL地址
                URL url = new URL(completeUrlString);
                //请求对象
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //设置请求方法
                //参数提交
                //提交的数据有PhoneName,password
                connection.setRequestMethod("POST");
                //连接
                connection.connect();
                Log.i("aaaa",connection.getResponseCode()+"");
                //判断请求码
                if (connection.getResponseCode() == 200) {
                    StringBuilder stringBuilder = new StringBuilder();
                    String line = null;
                    //输入流读取
                    InputStream inputStream = connection.getInputStream();
                    //高效字节流
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                    while ((line = br.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    //把读取的内容转换成字串
                    return stringBuilder.toString();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);
            //数据回传给P层,接口毁掉
            Log.e("NowAysncTask",response);
            callBack.responseData(response);
        }
    }
}

LoginPresenter

public class LoginPresenterImpl implements LoginContract.LoginPresenter{
    LoginContract.LoginView loginView;
    private SoftReference<LoginContract.LoginView> softReference;
    private LoginModelImpl model;

    @Override
    public void attachView(LoginContract.LoginView loginView) {
        this.loginView = loginView;
        //软引用包裹
        softReference = new SoftReference<>(loginView);
        //创建M层
        model = new LoginModelImpl();
    }

    @Override
    public void detachView(LoginContract.LoginView loginView) {
        softReference.clear();
    }

    @Override
    public void requestLoginData(String phone, String pass) {
        model.containLoginResponseData(phone,pass,new LoginContract.LoginModel.CallBack(){
            @Override
            public void responseData(String response) {
                loginView.showData(response);
            }
        });
    }
}

RegPresenterImpl

public class RegPresenterImpl implements RegContract.RegPresenter {
    RegContract.RegView regView;
    private SoftReference<RegContract.RegView> softReference;
    private RegModelImpl model;

    @Override
    public void attachView(RegContract.RegView regView) {
        this.regView = regView;
        softReference = new SoftReference<>(regView);
        model = new RegModelImpl();
    }

    @Override
    public void detachView(RegContract.RegView regView) {
        softReference.clear();
    }

    @Override
    public void requestData(String phone, String pass) {
        model.containData(phone, pass, new RegContract.RegModel.CallBack() {
            @Override
            public void responseData(String s) {
                regView.showdata(s);
            }
        });
    }
}

今天就分享到这   欢迎大家在讨论区讨论  如果本人有什么写的不对的地方  还望大牛指点指点

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值