新建LoginInfo类
public class LoginInfo { private String loginName; public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String password;}
新建IUserBiz登录接口
public interface IUserBiz { void login(String username, String password, OnLoginListener loginListener);}
新建OnLoginListener接口
public interface OnLoginListener { void loginSuccess(LoginInfo loginInfo); void loginFailed();}
新建IUserLoginView接口
public interface IUserLoginView { String getLoginName(); String getPassword(); void showSuccess(); void showFailedError();}
新增UserBiz类
public class UserBiz implements IUserBiz { @Override public void login(final String loginName, final String password, final OnLoginListener loginListener) { new Thread() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if ("123".equals(loginName) && "123".equals(password)) { LoginInfo user = new LoginInfo(); user.setLoginName(loginName); user.setPassword(password); loginListener.loginSuccess(user); } else { loginListener.loginFailed(); } } }.start(); }}
新增UserLoginPresenter类
public class UserLoginPresenter { private IUserBiz userBiz; private IUserLoginView userLoginView; private Handler handler = new Handler(); public UserLoginPresenter(IUserLoginView userLoginView) { this.userLoginView = userLoginView; this.userBiz = new UserBiz(); } public void login(){ userBiz.login(userLoginView.getLoginName(), userLoginView.getPassword(), new OnLoginListener() { @Override public void loginSuccess(LoginInfo loginInfo) { handler.post(new Runnable() { @Override public void run() { userLoginView.showSuccess(); } }); } @Override public void loginFailed() { handler.post(new Runnable() { @Override public void run() { userLoginView.showFailedError(); } }); } }); }}
新增UserLoginActivity
public class UserLoginActivity extends AppCompatActivity implements IUserLoginView { EditText dtLoginName, dtPassword; Button btnLogin; UserLoginPresenter userLoginPresenter = new UserLoginPresenter(this); @Override public String getLoginName() { return dtLoginName.getText().toString(); } @Override public String getPassword() { return dtPassword.getText().toString(); } @Override public void showSuccess() { Toast.makeText(this, "成功", Toast.LENGTH_LONG).show(); } @Override public void showFailedError() { Toast.makeText(this, "失败", Toast.LENGTH_LONG).show(); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); dtLoginName = (EditText) findViewById(R.id.loginName); dtPassword = (EditText) findViewById(R.id.password); btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { userLoginPresenter.login(); } }); }}