android使用mvp模式实现登陆demo

来源:互联网 发布:破解 联想 网络控制 编辑:程序博客网 时间:2024/05/16 14:08

  最近读到《MVP模式的14条规则》一片关于mvp设计模式的文章 (处http://blog.csdn.net/qinjienj/article/details/6972248)。觉得用mvp会更利于android的界面和逻辑的拆分和单测的进行,试着写了一个登陆的demo。 

/** * 登陆界面 com.example.mvptest.LoginActivity *  * @author yangqinghai <br/> *         create at 2013-5-3 上午10:50:08 */public class LoginActivity extends Activity implements LoginView {  private static Context mContext;  /**   * 账号输入控件   */  private EditText mAccount;  /**   * 密码输入控件   */  private EditText mPassword;  /**   * 登陆按钮   */  private Button mLogin;  /**   * 登陆Presenter   */  private LoginPresenter loginPresenter;  private LoginHandler mLoginHandler = new LoginHandler();  public static class LoginHandler extends Handler {    @Override    public void handleMessage(Message msg) {      super.handleMessage(msg);      switch (msg.what) {        case 1:          Toast.makeText(mContext, "用户名错误", Toast.LENGTH_LONG).show();          break;        case 2:          Toast.makeText(mContext, "密码错误", Toast.LENGTH_LONG).show();          break;        case 3:          Toast.makeText(mContext, "账号异常,请稍后重试", Toast.LENGTH_LONG).show();          break;      }    }  }  @Override  protected void onCreate(Bundle savedInstanceState) {    mContext = this;    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_login);    initView();    loginPresenter = new LoginPresenter(this);  }  public void initView() {    mAccount = (EditText) findViewById(R.id.login_account);    mPassword = (EditText) findViewById(R.id.login_password);    mLogin = (Button) findViewById(R.id.login_loginButton);  }  public void onLoginClick(View view) {    LoginBean loginInfo = new LoginBean();    loginInfo.mAccount = mAccount.getText().toString();    loginInfo.mPassword = mPassword.getText().toString();    loginPresenter.handleClick(loginInfo);  }  @Override  public void reFresh(int reFreshCode) {    Message message = new Message();    message.what = reFreshCode;    mLoginHandler.sendMessage(message);  }  @Override  public <T> void onLoginClick(T t) {  }  @Override  public void goToMainActivity() {    Intent intent = new Intent(LoginActivity.this, MainActivity.class);    startActivity(intent);  }}

/** * 登陆信息Bean com.example.mvptest.bean.LoginBean *  * @author yangqinghai <br/> *         create at 2013-5-3 下午3:44:08 */public class LoginBean {  private static final String TAG = "LoginBean";  public String mAccount;  public String mPassword;}

/*** * 视图 ipresenter.Ipresenter *  * @author yangqinghai <br/> *         create at 2013-5-3 上午11:39:27 */public interface Ipresenter {  /**   * 处理点击事件   *    * @param t   */  public <T> void handleClick(T t);  public <T> void handleHttpResult(T t);}

/** * 登陆 ipresenter.LoginPresenter *  * @author yangqinghai <br/> *         create at 2013-5-3 下午1:25:36 */public class LoginPresenter implements Ipresenter {  private static final String TAG = "LoginPresenter";  private Iview iDisplay;  public LoginPresenter(Iview display) {    iDisplay = display;  }  @Override  public <T> void handleClick(T t) {    LoginBean loginBean = (LoginBean) t;    new LoginModle(this).startLoginRequest(loginBean);  }  @Override  public <T> void handleHttpResult(T t) {    int resultCode = (Integer) t;    LoginView loginView = (LoginView) iDisplay;    if (resultCode == 0) {      loginView.goToMainActivity();    } else {      loginView.reFresh(resultCode);    }  }}

/** * 登陆modle com.example.mvptest.modle.LoginModle *  * @author yangqinghai <br/> *         create at 2013-5-3 下午4:35:12 */public class LoginModle extends Thread {  private static final String TAG = "LoginModle";  private Ipresenter iPresenter;  private LoginBean mLoginBean;  /**   * 结果码 。方便测试用,声明成static   */  private static int ResultCode;  public LoginModle(Ipresenter p) {    iPresenter = p;  }  /**   * 开启登陆请求   */  public void startLoginRequest(LoginBean loginBean) {    mLoginBean = loginBean;    start();  }  @Override  public void run() {    super.run();    // 休眠3000毫秒,标示登陆处理过程    try {      Thread.sleep(3000);    } catch (InterruptedException e) {      Log.e(TAG, "", e);    }    // 根据实际情况返回结果代码    iPresenter.handleHttpResult(ResultCode);    ResultCode += 1;  }}
 

/** * 视图view接口 定义 com.example.mvptest.iview.Iview *  * @author yangqinghai <br/> *         create at 2013-5-3 上午10:55:51 */public interface Iview {  /** 刷新界面 **/  public void reFresh(int reFreshCode);}


public interface LoginView extends Iview {  /**   * 登陆处理   *    * @param t   */  public <T> void onLoginClick(T t);  public void goToMainActivity();}

 

	
				
		
原创粉丝点击