mvp简单登录界面+抽基类
public class MainActivity extends BaseActivity implements LoginView {
private EditText edit1,edit2;
private Button btn;
private LoginPresenter mLoginPresenter;
private ProgressBar bar;
@Override
public void initView() {
edit1 = findViewById(R.id.edit1);
edit1 = findViewById(R.id.edit1);
bar = findViewById(R.id.bar);
btn = findViewById(R.id.btn);
}
@Override
public void initData() {
}
@Override
public int initLayoutId() {
return R.layout.activity_main;
}
@Override
public void initListener() {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edit1.getText().toString();
String pwd = edit1.getText().toString();
mLoginPresenter.login(name,pwd);
}
});
}
@Override
public BaseModel initPresenter() {
mLoginPresenter = new LoginPresenter(MainActivity.this,this);
return mLoginPresenter;
}
@Override
public BaseView initIView() {
return this;
}
@Override
public void onSucess(String result) {
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}
@Override
public void onFailer(String msg) {
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
@Override
public void show() {
bar.setVisibility(View.VISIBLE);
}
@Override
public void hide() {
bar.setVisibility(View.GONE);
}
@Override
public void onKong() {
Toast.makeText(this,"账号密码不能为空",Toast.LENGTH_SHORT).show();
}
}
public interface LoginView extends BaseView {
void onKong();
}
public class LoginPresenter extends BaseModel {
private LoginView mLoginView;
private LoginModel mLoginModel;
public LoginPresenter(Context context,LoginView mLoginView) {
super(context);
this.mLoginView = mLoginView;
mLoginModel = new LoginModel();
}
public void login(String name,String pwd){
BaseView baseView = getBaseView();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
mLoginView.onKong();
}
mLoginView.show();
mLoginModel.login(name,pwd, new LoginBiz() {
@Override
public void onLoginSucess(String result) {
mLoginView.onSucess(result);
mLoginView.hide();
}
@Override
public void onLoginFailer(String msg) {
mLoginView.onFailer(msg);
mLoginView.hide();
}
});
}
}
public class LoginModel {
public void login(final String name, final String pwd, final LoginBiz loginView){
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String… strings) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
if(name.equals("123")||pwd.equals("123")){
loginView.onLoginSucess("登陆成功");
}else{
loginView.onLoginFailer("登录失败");
}
super.onPostExecute(s);
}
}.execute("");
}
}
public interface LoginBiz extends BaseBiz {
}
//baseactivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public abstract class BaseActivity extends AppCompatActivity {
private BaseModel mBaseModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int layoutId = initLayoutId();
if(layoutId!=0){
setContentView(layoutId);
initView();
initData();
initListener();
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mBaseModel = initPresenter();
mBaseModel.attach(initIView());
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mBaseModel.detach();
mBaseModel=null;
}
public abstract void initView();
public abstract void initData();
public abstract int initLayoutId();
public abstract void initListener();
public abstract BaseModel initPresenter();
public abstract BaseView initIView();
}
//抽取view层
public interface BaseView {
void onSucess(String result);
void onFailer(String msg);
void show();
void hide();
}
//抽取biz层
public interface BaseBiz {
void onLoginSucess(String result);
void onLoginFailer(String msg);
}
//抽取model层
public class BaseModel {
private BaseView mBaseView;
private Context Context;
public BaseModel( Context context) {
this.Context = context;
}
public BaseView getBaseView() {
return mBaseView;
}
public Context getContext() {
return Context;
}
public void attach(BaseView baseView) {
this.mBaseView = baseView;
}
public void detach() {
mBaseView=null;
Context=null;
}
}