MVP登录注册

登录的布局

 <LinearLayout
        android:layout_marginLeft="50dp"
        android:layout_marginTop="160dp"
        android:layout_marginRight="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="25dp"
            android:layout_height="35dp"
            android:background="@drawable/login_icon_phone_n_hdpi"/>
        <EditText
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:id="@+id/login_phone"
            android:hint="手机号"
            android:textColorHint="#ffffff"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <View
        android:layout_marginTop="13dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="2dp"/>
    <LinearLayout
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="25dp"
            android:layout_height="35dp"
            android:background="@drawable/login_icon_lock_n_hdpi"/>
        <EditText
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:id="@+id/login_password"
            android:hint="登录密码"
            android:layout_weight="1"
            android:textColorHint="#ffffff"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/login_eye_check"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/login_icon_eye_n_hdhpi"
            android:layout_width="30dp"
            android:layout_height="30dp" />
    </LinearLayout>
    <View
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="2dp"/>
    <LinearLayout
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/login_jizhu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="记住密码"
            android:textSize="14sp"
            android:textColor="#ffffff"
            android:buttonTint="#FFF"/>
        <TextView
            android:id="@+id/login_zhuce"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="快速注册"
            android:textSize="14sp"
            android:textColor="#ffffff"
            android:buttonTint="#ffffff"/>
    </LinearLayout>
    <Button
        android:id="@+id/login_button"
        android:layout_marginTop="50dp"
        android:layout_width="280dp"
        android:layout_height="40dp"
        android:background="@drawable/shape_radio"
        android:text="登录"
        android:textSize="18sp"
        android:textColor="#ff5f71"
        android:layout_gravity="center"
        />

首先写一下我们的接口
也就是返回在V层的数据


public interface ILoginMainView extends BaseView{

    void ouSeccuess(LoginBean loginBean);
}

我们登录的代码模块包括 sp存储 记住密码 登录注册

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginPassword.setInputType(129);
        sp = getSharedPreferences("yxx",MODE_PRIVATE);
        boolean ck = sp.getBoolean("ck", false);
        if(ck){
            String phone1 = sp.getString("phone", null);
            String pwd1 = sp.getString("pwd", null);
            loginJizhu.setChecked(true);
            loginPhone.setText(phone1);
            loginPassword.setText(pwd1);
        }
    }
    @OnClick({R.id.login_eye_check, R.id.login_jizhu, R.id.login_zhuce, R.id.login_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_eye_check:
                if (loginPassword.getInputType() == 129){
                    loginPassword.setInputType(127);
                }else {
                    loginPassword.setInputType(129);
                }
                break;
            case R.id.login_zhuce:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            case R.id.login_button:
                 phone = loginPhone.getText().toString();
                 pwd = loginPassword.getText().toString();
                mLoginMap = new HashMap<>();
                mLoginMap.put("phone", this.phone);
                mLoginMap.put("pwd", this.pwd);
                loginPresenter = new LoginPresenter();
                loginPresenter.setView(this);
                loginPresenter.ShowData(IConstans.LOGIN_URl,mLoginMap);
                break;
        }
    }


    @Override
    public void ouSeccuess(LoginBean loginBean) {

        Toast.makeText(this,loginBean.getMessage(),Toast.LENGTH_SHORT).show();
        if (loginBean.getMessage().equals("登录成功")){
            edit = sp.edit();
            edit.putString("phone",phone);
            edit.putString("pwd",pwd);
            edit.putBoolean("ck",true);
            edit.commit();
            startActivity(new Intent(LoginActivity.this,MainActivity.class));
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.deattch();
    }

接下来就是P层的逻辑代码了

public class LoginPresenter extends BasePresenter<ILoginMainView> implements HttpLoginUtils.CallbackData<String> {

    private HttpLoginUtils loginUtils;

    public LoginPresenter(){
        loginUtils = HttpLoginUtils.getInstence();
        loginUtils.setCallBackInstence(this);
    };

    public void ShowData(String url,Map<String,String> mLoginMap){
        loginUtils.getpost(url,mLoginMap);
    }

    @Override
    public void onResponse(String data) {
       Gson gson =  new Gson();
       LoginBean loginBean = gson.fromJson(data,LoginBean.class);
       getView().ouSeccuess(loginBean);
    }

    @Override
    public void onFailure(IOException e) {

    }

}

写完逻辑就该写我们抽取的基类 每次新建一个模块的P层我们就可以继承一下


public class BasePresenter<V> {

    private V view;

    public void setView(V view) {
        this.view = view;
    }

    public void deattch(){
        this.view = null;
    }

    public V getView(){
        return view;
    }
}

这个就是我们的HttpUtils 这里有我们的拦截器 和数据的返回我们使用的是接口回调和Handler来实现的

    private static HttpLoginUtils httpLoginUtils;
    CallbackData callbackData;


    public void setCallBackInstence(CallbackData callBackInstence){
        this.callbackData = callBackInstence;
    }
    private  HttpLoginUtils(){};

    public static HttpLoginUtils getInstence(){
        if(httpLoginUtils == null){
            return  new HttpLoginUtils();
        }else{
            return httpLoginUtils;
        }
    }
    public void getpost(String url, Map<String,String> mLoginMap) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(3000, TimeUnit.MILLISECONDS)
                .readTimeout(3000, TimeUnit.MILLISECONDS)
                .writeTimeout(3000, TimeUnit.MILLISECONDS)
                .build();


        FormBody.Builder builder = new FormBody.Builder();
        for (String key:mLoginMap.keySet()){
            builder.add(key,mLoginMap.get(key));
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder().post(formBody).url(url).build();
        okhttp3.Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callbackData.onFailure(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Message message = handler.obtainMessage();
                message.obj = string;
                handler.sendMessage(message);
            }
        });
    }


    public interface CallbackData<D> {
        void onResponse(D t);

        void onFailure(IOException e);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Object obj = msg.obj;
            callbackData.onResponse(obj);
        }
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值