https://github.com/j2eemail/MVP-Login
public class UserModle{
private String userName;
private String passWord;
public void setUserName(String userName) {
// TODO Auto-generated method stub
this.userName = userName;
}
public void setPassWord(String passWord) {
// TODO Auto-generated method stub
this.passWord = passWord;
}
public String getUserName() {
// TODO Auto-generated method stub
return userName;
}
public String getPassWord() {
// TODO Auto-generated method stub
return passWord;
}
}
public interface IView {
}
public interface ILoginView extends IView {
String getUserName();
String getPassWord();
void clearUserName();
void clearPassword();
void showLoading();
void hideLoading();
void isEmpty();
void loginSuccess(UserModle userModle);
void loginFail();
}
public interface IPresenter {
public void clear();
public boolean isEmpty();
}
public abstract class BasePresenter<T> implements IPresenter {
public IView view;
public BasePresenter(IView view) {
this.view = view;
}
@Override
public void clear() {
// TODO Auto-generated method stub
view = null;
}
public abstract T getView();
public abstract void login();
public abstract void clearInput();
}
public class LoginPresenter extends BasePresenter<ILoginView> {
private ILoginNetWork loginNetWork;
public LoginPresenter(ILoginView loginView) {
super(loginView);
// TODO Auto-generated constructor stub
loginNetWork = new LoginNetWork();
}
@Override
public boolean isEmpty() {
ILoginView loginView = getView();
if (loginView.getUserName().isEmpty() || loginView.getPassWord().isEmpty()) {
return true;
} else {
return false;
}
}
@Override
public void login() {
// TODO Auto-generated method stub
final ILoginView loginView = getView();
if (isEmpty()) {
loginView.isEmpty();
return;
}
loginView.showLoading();
loginNetWork.login(loginView.getUserName(), loginView.getPassWord(), new ILoginListener() {
@Override
public void loginSuccess(final UserModle userModle) {
// TODO Auto-generated method stub
loginView.loginSuccess(userModle);
loginView.hideLoading();
}
@Override
public void loginFail() {
// TODO Auto-generated method stub
loginView.loginFail();
loginView.hideLoading();
}
});
}
@Override
public void clearInput() {
// TODO Auto-generated method stub
ILoginView loginView = getView();
loginView.clearUserName();
loginView.clearPassword();
}
@Override
public void clear() {
loginNetWork.removeRequest();
loginNetWork = null;
super.clear();
}
@Override
public ILoginView getView() {
return (ILoginView) view;
}
}
public interface ILoginListener {
void loginSuccess(UserModle userModle);
void loginFail();
}
public interface ILoginNetWork {
public void login(String userName, String passWord, ILoginListener loginListener);
public void removeRequest();
}
public class LoginNetWork implements ILoginNetWork {
private MyAsyncTask myAsyncTask;
@Override
public void login(String userName, String passWord, ILoginListener loginListener) {
// TODO Auto-generated method stub
myAsyncTask = new MyAsyncTask(userName, passWord, loginListener);
myAsyncTask.execute();
}
@Override
public void removeRequest() {
myAsyncTask.cancel(true);
myAsyncTask = null;
}
private static class MyAsyncTask extends AsyncTask {
String userName;
String passWord;
ILoginListener loginListener;
public MyAsyncTask(String userName, String passWord, ILoginListener loginListener) {
this.userName = userName;
this.passWord = passWord;
this.loginListener = loginListener;
}
@Override
protected Object doInBackground(Object[] params) {
try {
//模拟耗时操作
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if ("name".equals(userName) && "123".equals(passWord)) {
UserModle userModle = new UserModle();
userModle.setUserName(userName);
userModle.setPassWord(passWord);
loginListener.loginSuccess(userModle);
} else {
loginListener.loginFail();
}
}
}
}
public class MainActivity extends AppCompatActivity implements ILoginView {
private LoginPresenter loginPresenter;
private ProgressDialog dialog;
private EditText view_username;
private EditText view_password;
private Button view_login;
private Button view_clear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
loginPresenter = new LoginPresenter(this);
view_username = (EditText) findViewById(R.id.view_username);
view_password = (EditText) findViewById(R.id.view_password);
view_login = (Button) findViewById(R.id.view_login);
view_clear = (Button) findViewById(R.id.view_clear);
view_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (loginPresenter != null) {
loginPresenter.login();
}
}
});
view_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (loginPresenter != null) {
loginPresenter.clearInput();
}
}
});
}
@Override
public String getUserName() {
// TODO Auto-generated method stub
return view_username.getText().toString();
}
@Override
public String getPassWord() {
// TODO Auto-generated method stub
return view_password.getText().toString();
}
@Override
public void clearUserName() {
// TODO Auto-generated method stub
view_username.setText("");
}
@Override
public void clearPassword() {
// TODO Auto-generated method stub
view_password.setText("");
}
@Override
public void showLoading() {
// TODO Auto-generated method stub
dialog.show();
}
@Override
public void hideLoading() {
// TODO Auto-generated method stub
dialog.dismiss();
}
@Override
public void isEmpty() {
// TODO Auto-generated method stub
show("请输入用户名和密码!");
}
@Override
public void loginSuccess(UserModle userBean) {
// TODO Auto-generated method stub
show("登陆成功!");
}
@Override
public void loginFail() {
// TODO Auto-generated method stub
show("登陆失败!");
}
private void show(String str) {
Log.d("CESHI", str);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginPresenter.clear();
loginPresenter = null;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/view_username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text" />
<EditText
android:id="@+id/view_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textPassword" />
<Button
android:id="@+id/view_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="登陆"
/>
<Button
android:id="@+id/view_clear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="清除"
/>
</LinearLayout>