mvp简单登录界面+抽基类

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;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值