写点什么

用 MVP 模式构建 Android 代码

用户头像
Android架构
关注
发布于: 5 小时前

public class UserBiz implements IUserBiz{


@Override


public void login(final String username,final String password,final OnLoginListener loginListener){


//模拟子线程耗时操作


new Thread()


{


@Override


public void run()


{


try


{


Thread.sleep(2000);


} catch (InterruptedException e)


{


e.printStackTrace();


}


//模拟登录成功


if ("wxh".equals(username) && "123".equals(password))


{


User user = new User();


user.setUsername(username);


user.setPassword(password);


loginListener.loginSuccess(user);


} else


{


loginListener.loginFailed();


}


}


}.start();


}


}

(二)Presenter

package com.example.administrator.mvp.presenter;


import android.os.Handler;


import com.example.administrator.mvp.bean.User;


import com.example.administrator.mvp.biz.IUserBiz;


import com.example.administrator.mvp.biz.OnLoginListener;


import com.example.administrator.mvp.biz.UserBiz;


import com.example.administrator.mvp.view.IUserLoginView;


public class UserLoginPresenter {


private IUserBiz userBiz;


private IUserLoginView userLoginView;


private Handler mHandler = new Handler();


public UserLoginPresenter(IUserLoginView userLoginView)


{


this.userLoginView = userLoginView;


this.userBiz = new UserBiz();


}


public void login()


{


userLoginView.showLoading();


userBiz.login(userLoginView.getUserName(), userLoginView.getPassword(), new OnLoginListener()


{


@Override


public void loginSuccess(final User user)


{


//需要在 UI 线程执行


mHandler.post(new Runnable()


{


@Override


public void run()


{


userLoginView.toMainActivity(user);


userLoginView.hideLoading();


}


});


}


@Override


public void loginFailed()


{


//需要在 UI 线程执行


mHandler.post(new Runnable()


{


@Override


public void run()


{


userLoginView.showFailedError();


userLoginView.hideLoading();


}


});


}


});


}


public void clear()


{


userLoginView.clearUserName();


userLoginView.clearPassword();


}


}

(三)View

package com.example.administrator.mvp.view;


import com.example.administrator.mvp.bean.User;


public interface IUserLoginView {


public String getUserName();


public String getPassword();


public void clearUserName();


public void clearPassword();


public void showLoading();


public void hideLoading();


public void toMainActivity(User user);


public void showFailedError();


}


package com.example.administrator.mvp.view;


import android.os.Bundle;


import android.support.v7.app.AppCompatActivity;


import android.view.View;


import android.widget.Button;


import android.widget.EditText;


import android.widget.ProgressBar;


import android.widget.Toast;


import com.example.administrator.mvp.R;


import com.example.administrator.mvp.bean.User;


import com.example.administrator.mvp.presenter.UserLoginPresenter;


public class UserLoginActivity extends AppCompatActivity implements IUserLoginView{


private EditText mEtUsername,mEtPassword;


private Button mBtnLogin,mBtnClear;


private ProgressBar mPbLoading;


private UserLoginPresenter mUserLoginPresenter = new UserLoginPresenter(this);


@Override


protected void onCreate(Bundle savedInstanceState)


{


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_user_login);


initViews();


}


private void initViews()


{


mEtUsername = (EditText) findViewById(R.id.username);


mEtPassword = (EditText) findViewById(R.id.password);


mBtnClear = (Button) findViewById(R.id.clear);


mBtnLogin = (Button) findViewById(R.id.login);


mPbLoading = (ProgressBar) findViewById(R.id.loading);


mBtnLogin.setOnClickListener(new View.OnClickListener()


{


@Override


public void onClick(View v)


{


mUserLoginPresenter.login();


}


});


mBtnClear.setOnClickListener(new View.OnClickListener()


{


@Override


public void onClick(View v)


{


mUserLoginPresenter.clear();


}


});


}


@Override


public String getUserName()


{


return mEtUsername.getText().toString();


}


@Override


public String getPassword()


{


return mEtPassword.getText().toString();


}


@Override


public void clearUserName()


{


mEtUsername.setText("


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


");


}


@Override


public void clearPassword()


{


mEtPassword.setText("");


}


@Override


public void showLoading()


{


mPbLoading.setVisibility(View.VISIBLE);


}


@Override


public void hideLoading()


{


mPbLoading.setVisibility(View.GONE);


}


@Override


public void toMainActivity(User user)


{


Toast.makeText(this, user.getUsername() +


" login success , to MainActivity", Toast.LENGTH_SHORT).show();


}


@Override


public void showFailedError()


{


Toast.makeText(this,


"login failed", Toast.LENGTH_SHORT).show();


}


}

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
用MVP模式构建Android代码