一、MVP关系
项目结构
二、项目
1.AndroidManifest.xml文件和bean
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mvp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.view.LoginViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.view.MainActivity"/>
</application>
</manifest>
新建一个User对象
package com.bean;
/**
* Created by c9736 on 2016/7/17.
*/
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.view
package com.view;
/**
* Created by c9736 on 2016/7/14.
*/
public interface ILoginView {
void onClearText();
String getUserName();
String getPassword();
void showLoading();
void hideLoading();
void toMainActivity();
void showFaildError();
}
package com.view;
import android.content.Intent;
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.mvp.R;
import com.presenter.LoginPresenter;
/**
* Created by c9736 on 2016/7/17.
*/
public class LoginViewActivity extends AppCompatActivity implements ILoginView{
private EditText mEtUsername, mEtPassword;
private Button mBtnLogin, mBtnClear;
private ProgressBar mPbLoading;
private LoginPresenter mUserLoginPresenter = new LoginPresenter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
}
private void initViews() {
//find view
mEtUsername = (EditText) this.findViewById(R.id.username);
mEtPassword = (EditText) this.findViewById(R.id.password);
mBtnLogin = (Button) this.findViewById(R.id.login);
mPbLoading = (ProgressBar) this.findViewById(R.id.progressBar);
mBtnClear = (Button) this.findViewById(R.id.clear);
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUserLoginPresenter.doLogin(getUserName(),getPassword());
}
});
mBtnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUserLoginPresenter.clear();
}
});
}
@Override
public void onClearText() {
mEtUsername.setText("");
mEtPassword.setText("");
}
@Override
public String getUserName() {
return mEtUsername.getText().toString();
}
@Override
public String getPassword() {
return mEtPassword.getText().toString();
}
@Override
public void showLoading() {
mPbLoading.setVisibility(View.VISIBLE);
}
@Override
public void hideLoading() {
mPbLoading.setVisibility(View.GONE);
}
@Override
public void toMainActivity() {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
@Override
public void showFaildError() {
Toast.makeText(this,
"login failed", Toast.LENGTH_SHORT).show();
}
}
package com.view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.mvp.R;
import com.presenter.ILoginPresenter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
3.prsenter
package com.presenter;
/**
* Created by c9736 on 2016/7/14.
*/
public interface ILoginPresenter {
void clear();
void doLogin(String name,String password);
}
package com.presenter;
import com.bean.User;
/**
* Created by c9736 on 2016/7/17.
*/
public interface OnLoginListener {
void loginSuccess(User user);
void loginFailed();
}
package com.presenter;
import android.os.Handler;
import com.model.IUserBiz;
import com.bean.User;
import com.model.UserBiz;
import com.view.ILoginView;
/**从View中获取需要的参数,交给Model去执行业务方法,执行的过程中需要传入listener以得到反馈结果,再让View用handler进行做对应的显示。
* Created by c9736 on 2016/7/14.
*
*/
public class LoginPresenter implements ILoginPresenter{
ILoginView iLoginView;//用来操作view
private IUserBiz userBiz;//用来操作业务方法,获取数据等
Handler handler ;//用来更新view
public LoginPresenter(ILoginView iLoginView) {
this.iLoginView = iLoginView;
this.userBiz=new UserBiz();
this.handler=new Handler();
}
@Override
public void clear() {
iLoginView.onClearText();
}
@Override
public void doLogin(String name, String password) {
iLoginView.showLoading();
userBiz.login(iLoginView.getUserName(), iLoginView.getPassword(), new OnLoginListener() {
@Override
public void loginSuccess(final User user) {
//在UI线程中执行
handler.post(new Runnable() {
@Override
public void run() {
iLoginView.toMainActivity();
iLoginView.hideLoading();
}
});
}
@Override
public void loginFailed() {
handler.post(new Runnable() {
@Override
public void run() {
iLoginView.showFaildError();
iLoginView.hideLoading();
}
});
}
});
}
}
4 model
package com.model;
import com.presenter.OnLoginListener;
/**
* Created by c9736 on 2016/7/17.
*/
public interface IUserBiz {
public void login(String username,String password,OnLoginListener loginListener);
}
package com.model;
import com.bean.User;
import com.presenter.OnLoginListener;
/**
* Created by c9736 on 2016/7/17.
*/
public class UserBiz implements IUserBiz{//用来操作逻辑业务,获取图片等数据
@Override
public void login(final String username, final String password, final OnLoginListener loginListener) {
new Thread(){
@Override
public void run() {
super.run();
try {
Thread.sleep(2000);
}catch (InterruptedException e){
e.printStackTrace();
}
if("chen".equals(username)&&"111".equals(password)){
User user=new User(username,password);
loginListener.loginSuccess(user);
}else {
loginListener.loginFailed();
}
}
}.start();
}
}