博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android MVP框架实现登录案例
阅读量:5163 次
发布时间:2019-06-13

本文共 5920 字,大约阅读时间需要 19 分钟。

一、Model

package com.czhappy.mvpdemo.model;/** * author: chenzheng * created on: 2019/5/16 11:06 * description: */public class User {    public String username;    public String password;}
package com.czhappy.mvpdemo.biz;import com.czhappy.mvpdemo.model.User;/** * author: chenzheng * created on: 2019/5/16 11:08 * description: */public interface UserLoginListener {    public void loginSuccess(User user);    public void loginFailed();}
package com.czhappy.mvpdemo.biz;/** * author: chenzheng * created on: 2019/5/16 11:07 * description: */public interface IUserLoginBiz {    public void login(String username, String password, UserLoginListener userLoginListener);}
package com.czhappy.mvpdemo.biz;import com.czhappy.mvpdemo.model.User;/** * author: chenzheng * created on: 2019/5/16 11:10 * description: */public class UserLoginBiz implements IUserLoginBiz {    @Override    public void login(final String username, final String password, final UserLoginListener userLoginListener) {        new Thread() {            @Override            public void run() {                super.run();                try {                    Thread.sleep(2000);                    if ("admin".equals(username) && "123".equals(password)) {                        User user = new User();                        user.username = username;                        user.password = password;                        userLoginListener.loginSuccess(user);                    } else {                        userLoginListener.loginFailed();                    }                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }.start();    }}

二、View

package com.czhappy.mvpdemo.view;import com.czhappy.mvpdemo.model.User;/** * author: chenzheng * created on: 2019/5/16 11:26 * description: */public interface IUserLoginView {    public void showLoading();    public void hideLoading();    public String getUsername();    public String getPassword();    public void toMainActivity(User user);    public void showLoginFailed();}
package com.czhappy.mvpdemo.activity;import android.os.Bundle;import android.support.annotation.Nullable;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.czhappy.mvpdemo.R;import com.czhappy.mvpdemo.model.User;import com.czhappy.mvpdemo.presenter.UserLoginPresenter;import com.czhappy.mvpdemo.view.IUserLoginView;/** * author: chenzheng * created on: 2019/5/16 10:41 * description: */public class LoginActivity extends AppCompatActivity implements IUserLoginView{    private EditText username_et, password_et;    private Button login_btn;    private ProgressBar progressBar;    private UserLoginPresenter userLoginPresenter = new UserLoginPresenter(this);    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);                initView();    }    private void initView() {        username_et = findViewById(R.id.username_et);        password_et = findViewById(R.id.password_et);        login_btn = findViewById(R.id.login_btn);        progressBar = findViewById(R.id.progressBar);        login_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                userLoginPresenter.login();            }        });    }    @Override    public void showLoading() {        progressBar.setVisibility(View.VISIBLE);    }    @Override    public void hideLoading() {        progressBar.setVisibility(View.GONE);    }    @Override    public String getUsername() {        return username_et.getText().toString();    }    @Override    public String getPassword() {        return password_et.getText().toString();    }    @Override    public void toMainActivity(User user) {        Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();    }    @Override    public void showLoginFailed() {        Toast.makeText(this, "登录失败", Toast.LENGTH_LONG).show();    }}

三、Presenter

Presenter是用作Model和View之间交互的桥梁

package com.czhappy.mvpdemo.presenter;import android.os.Handler;import com.czhappy.mvpdemo.biz.IUserLoginBiz;import com.czhappy.mvpdemo.biz.UserLoginBiz;import com.czhappy.mvpdemo.biz.UserLoginListener;import com.czhappy.mvpdemo.model.User;import com.czhappy.mvpdemo.view.IUserLoginView;/** * author: chenzheng * created on: 2019/5/16 11:13 * description: */public class UserLoginPresenter {    private IUserLoginBiz userLoginBiz;    private IUserLoginView userLoginView;    private Handler mHandler = new Handler();    public UserLoginPresenter(IUserLoginView userLoginView) {        this.userLoginView = userLoginView;        userLoginBiz = new UserLoginBiz();    }    public void login(){        userLoginView.showLoading();        userLoginBiz.login(userLoginView.getUsername(), userLoginView.getPassword(), new UserLoginListener() {            @Override            public void loginSuccess(final User user) {                //需要在UI线程执行                mHandler.post(new Runnable()                {                    @Override                    public void run()                    {                        userLoginView.toMainActivity(user);                        userLoginView.hideLoading();                    }                });            }            @Override            public void loginFailed() {                //需要在UI线程执行                mHandler.post(new Runnable()                {                    @Override                    public void run()                    {                        userLoginView.showLoginFailed();                        userLoginView.hideLoading();                    }                });            }        });    }}

 

四、实现效果

转载于:https://www.cnblogs.com/chenzheng8975/p/10900588.html

你可能感兴趣的文章
after()和inserAfter(),before()和inserBefore()区别
查看>>
JDBC——释放资源的代码
查看>>
bootstrap模态框垂直居中
查看>>
用数据管理过程(3)——可预测级别的量化管理(麦当劳的管理方式)
查看>>
DataGridView的Validating事件注册后删除操作的处理
查看>>
我的IOS学习历程-第七天
查看>>
json的两种表示结构(对象和数组).。
查看>>
iOS Undefined symbols for architecture xxx问题的总结
查看>>
bzoj 3685: 普通van Emde Boas树
查看>>
关于线程池,那些你还不知道的事
查看>>
二分类问题F-score评判指标(转载)
查看>>
JAVA基础之字符串和面向对象
查看>>
大数据R语言简析
查看>>
Ant自己主动编译打包&公布 android项目
查看>>
vc6编译stlport及常见问题
查看>>
week_one-python格式化输出
查看>>
Webservice 从客户端中检测到有潜在危险的 request.form值[解决方法]
查看>>
LNMP详细介绍
查看>>
素数判定算法
查看>>
那些文章
查看>>