前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android登录代码MVP架构详解

Android登录代码MVP架构详解

作者头像
砸漏
发布2020-11-04 15:30:21
3810
发布2020-11-04 15:30:21
举报
文章被收录于专栏:恩蓝脚本

登录代码MVP架构的具体实现,分享给大家。

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Mvp.fff {

  private EditText mUsername;
  private EditText mPassword;
  private Button login;
  private ProgressDialog mProgressDialog;
  private Mvp mMvp;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

     mMvp = new Mvp(this);

  }
  private void initView() {
    mUsername = (EditText) findViewById(R.id.username);
    mPassword = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);

    login.setOnClickListener(this);

    //动态的创建一个进度条
    mProgressDialog = new ProgressDialog(this);
  }

  @Override
  public void onClick(View view) {

    switch (view.getId()){
      case R.id.login:
        String user = mUsername.getText().toString();
        String pass = mPassword.getText().toString();
        final User users = new User();
        users.username=user;
        users.password=pass;


        Boolean empty = mMvp.isEmpty(users);
        if (empty){

          mProgressDialog.show();
          mMvp.isLogin(users);
        }else {

          Toast.makeText(MainActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
          return;
        }

        break;
    }

  }

  @Override
  public void success() {

    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        mProgressDialog.dismiss();
        Toast.makeText(MainActivity.this, "欢迎回来,思密达", Toast.LENGTH_SHORT).show();
      }
    });
  }

  @Override
  public void doil() {

    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        mProgressDialog.dismiss();
        Toast.makeText(MainActivity.this, "登录失败,账号密码有问题", Toast.LENGTH_SHORT).show();
      }
    });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mMvp=null;
  }
}

UserLogin.java

代码语言:javascript
复制
public class UserLogin {  //MVC Controller层

  public boolean login(User users){
    SystemClock.sleep(1000);


    if ("frq".equals(users.username)&&"frq".equals(users.password)){
      return true;
    }else {
      return false;
    }

  }
}

Mvp.java

代码语言:javascript
复制
public class Mvp {  //MVP Precenter层

  public Mvp(fff fff) {
    mFff = fff;
  }

  public Boolean isEmpty(User users){
    if (TextUtils.isEmpty(users.username) || TextUtils.isEmpty(users.password)){

      return false;
    }else {
      return true;
    }
  }

  public void isLogin(final User users){
    new Thread(){
      @Override
      public void run() {
        UserLogin login = new UserLogin();
        boolean b = login.login(users);


        if (b){
          mFff.success();
        }else {
          mFff.doil();
        }

      }
    }.start();
  }

  public interface fff{
    void success();
    void doil();
  }

  private fff mFff;

}
===========================================
public class Precenter {
  public Precenter(fff fff) {
    mFff = fff;
  }

  public void As(String urlString){
    new AsyncTask<String, Void, ArrayList<FirstBean.ResultBean  () {
      @Override
      protected ArrayList<FirstBean.ResultBean  doInBackground(String... strings) {

        String s = new HttpUrl().toString(strings[0]); //调用外部类

        FirstBean firstBean = new Gson().fromJson(s, FirstBean.class); //gson解析
        if (firstBean!=null){
          return (ArrayList<FirstBean.ResultBean ) firstBean.getResult();
        }
        return null;

      }

      @Override
      protected void onPostExecute(ArrayList<FirstBean.ResultBean  resultBeans) {
        super.onPostExecute(resultBeans);
        if (resultBeans!=null){
          mFff.success(resultBeans);
        }else {
          mFff.bai();
        }

      }
    }.execute(urlString);
  }

  public interface fff{

    void success(ArrayList<FirstBean.ResultBean  resultBeans);
    void bai();
  };
  private fff mFff;
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档