Android 使用MVP实现登录功能

**

Contrant

**

package com.example.administrator.xiangmu.di.contrant;

public interface Contrant {

    //登录的V层
    public interface IDengView{

        public void showData(String responseData);

        public void jumpActivity();
    }

    //登录的P层
    public interface IDengPresenter<IDengView>{

        public void attachView(Contrant.IDengView dengView);

        public void detachView(Contrant.IDengView dengView);

        public void requstLoginData(String userName, String password);
    }


    //登录的M层
    public interface IDengModel{

        public void containLoginResponseData(String userName, String password, CallBack callback);

        public interface CallBack {
            public void responseData(String responseData);
        }
    }


}

**

Model层

**

public class IDengModelImpl  implements Contrant.IDengModel {

    @Override
    public void containLoginResponseData(final String userName,final String password,final CallBack callback) {
        requestLoginDataEnqueue(userName, password, callback);

    }

    private void requestLoginDataEnqueue(String userName, String password, final CallBack callback) {

        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody formBody = new FormBody.Builder().build();
        Request request = new Request.Builder()
                .method("POST", formBody)
                .url(Constant.DENG_URL + "?phone=" + userName + "&pwd=" + password)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                String responseData = e.getMessage();
                callback.responseData(responseData);

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String responseData = response.body().string();
                callback.responseData(responseData);

            }
        });
    }
}

**

Presenter层

**

public class IDengPresenterImpl implements Contrant.IDengPresenter{

    Contrant.IDengView dengView;
    private SoftReference<Contrant.IDengView>  reference;
    private Contrant.IDengModel dengModel;


    @Override
    public void attachView(Contrant.IDengView dengView) {
        this.dengView = dengView;

        reference = new SoftReference<>(dengView);

        dengModel = new IDengModelImpl();
    }

    @Override
    public void detachView(Contrant.IDengView dengView) {
        reference.clear();
    }

    @Override
    public void requstLoginData(String userName, String password) {
        dengModel.containLoginResponseData(userName, password, new Contrant.IDengModel.CallBack() {
            @Override
            public void responseData(String responseData) {
                dengView.showData(responseData);
            }
        });
    }
}

**

main布局文件

**

<?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"
    android:background="@mipmap/zhuye_background"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/ed_dl_sj"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawablePadding="@dimen/dp_10"
        android:drawableLeft="@mipmap/login_icon_phone"
        android:layout_marginTop="160dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:background="@null"
        android:hint="手机号"
        android:textSize="18sp"
        android:textColorHint="#ffffff"
        android:textColor="@color/white"
        />
    <View
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fff"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        />

    <EditText
        android:id="@+id/ed_dl_pwd"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawablePadding="@dimen/dp_10"
        android:drawableLeft="@mipmap/login_icon_lock"
        android:drawableRight="@mipmap/login_icon_eye"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:background="@null"
        android:hint="登录密码"
        android:textSize="18sp"
        android:textColor="@color/white"
        android:textColorHint="#ffffff"
        android:inputType="textPassword"
        />
    <View
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fff"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        />
    
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="36dp"
        android:layout_marginRight="36dp"
        >
        <CheckBox
            android:id="@+id/jz_check"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="记住密码"
            android:textSize="18sp"
            android:textColor="#ffffff"
            android:buttonTint="#fff"
            />

        <TextView
            android:id="@+id/text_zc"
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="快速注册"
            android:textSize="18sp"
            android:textColor="#ffffff"
            android:button="@null"

            />

    </LinearLayout>

    <Button
        android:layout_marginBottom="120dp"
        android:id="@+id/btn_dl"
        android:layout_width="280dp"
        android:layout_height="60dp"
        android:text="登录"
        android:textSize="18sp"
        android:textColor="#ff5f71"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        />
</LinearLayout>

**

MainActivity

**

public class MainActivity extends AppCompatActivity implements Contrant.IDengView {

    @BindView(R.id.ed_dl_sj)
    EditText edDlSj;
    @BindView(R.id.ed_dl_pwd)
    EditText edDlPwd;
    @BindView(R.id.btn_dl)
    Button btnDl;
    @BindView(R.id.text_zc)
    TextView textZc;
    @BindView(R.id.jz_check)
    CheckBox jzCheck;
    private Contrant.IDengPresenter dengPresenter;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private Gson gson;
    private DengBean dengBean;

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

        dengPresenter = new IDengPresenterImpl();
        dengPresenter.attachView(this);

        preferences = getSharedPreferences("User", MODE_PRIVATE);
        editor = preferences.edit();

        boolean ischeck = preferences.getBoolean("ischeck", false);
        if (ischeck){

            String userName = edDlSj.getText().toString();
            String password = edDlPwd.getText().toString();

            edDlSj.setText(userName);
            edDlPwd.setText(password);
            jzCheck.setChecked(true);

        }

        textZc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ZhuceActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @OnClick(R.id.btn_dl)
    public void onViewClicked() {
        String userName = edDlSj.getText().toString();
        String password = edDlPwd.getText().toString();
        dengPresenter.requstLoginData(userName, password);
    }

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

    @Override
    public void showData(final String responseData) {
        gson = new Gson();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dengBean = gson.fromJson(responseData, DengBean.class);
                Toast.makeText(MainActivity.this, dengBean.getMessage(), Toast.LENGTH_SHORT).show();

                String userName = edDlSj.getText().toString();
                String password = edDlPwd.getText().toString();


                if (jzCheck.isChecked()) {

                    editor.putString("userName", userName);
                    editor.putString("password", password);

                    editor.putBoolean("ischeck", true);
                    editor.commit();
                }
                    if (dengBean.getStatus().equals("0000")) {
                        Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
                        startActivity(intent);
                    }
            }
        });
    }

    @Override
    public void jumpActivity() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (dengBean.getStatus().equals("0000")) {
                    Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
}

**

接口

**

public static final String DENG_URL = "http://mobile.bwstudent.com/small/user/v1/login";
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值