zoukankan      html  css  js  c++  java
  • Android MVP模式简单易懂的介绍方式 (三)

    Android MVP模式简单易懂的介绍方式 (一)

    Android MVP模式简单易懂的介绍方式 (二)

    Android MVP模式简单易懂的介绍方式 (三)

    讲完M和P,接下来就要讲V了。View也很好理解,就是把UI需要做的事情,通过接口暴露出来。那么UI要做什么事情,无非就是对UI控件的操作(这里是清空输入)、还有登陆成功或者失败的操作嘛。因此,我们可以这样写。

    public interface ILoginView {
    
        void onClearText();
        void onLoginReuslt(Boolean res,int code);
    }

    一样,有了接口,当然需要一个类来实现他。那么谁来实现这个接口呢?当然是我们的Activity。Activity实现了ILoginView接口后,由于逻辑的调用我们都在钱面写完了。因此,只需要写好UI处理即可。为了和ILoginPresenter 通讯,所以Activity需要一个ILoginPresenter ,然后,由ILoginPresenter 去和ILoginView进行通讯,从而实现UI与业务的分离

    public class LoginActivity extends AppCompatActivity implements ILoginView,View.OnClickListener{
    
        private EditText et_name;
        private EditText et_password;
        ILoginPresenter iLoginPresenter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.setTitle("登陆");
            findViewById(R.id.bt_login).setOnClickListener(this);
            findViewById(R.id.bt_clear).setOnClickListener(this);
            et_name = findViewById(R.id.et_username);
            et_password = findViewById(R.id.et_password);
    
            iLoginPresenter = new ILoginPresenterCompl(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case  R.id.bt_clear:
                    iLoginPresenter.clear();
                    break;
                case R.id.bt_login:
                    iLoginPresenter.doLogin(et_name.getText().toString(),et_password.getText().toString());
                    break;
                    default:
            }
        }
    
        @Override
        public void onClearText() {
            et_name.setText("");
            et_password.setText("");
        }
    
        @Override
        public void onLoginReuslt(Boolean res, int code) {
            switch (code){
                case 0:
                    break;
                case 1:
                    Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
                    break;
                case -1:
                    Toast.makeText(this,"用户名错误",Toast.LENGTH_SHORT).show();
                    break;
                case -2:
                    Toast.makeText(this,"密码错误",Toast.LENGTH_SHORT).show();
                    break;
                    default:
            }
        }
    
    }

    为了方便大家,顺便把布局文件也贴上,那么到这里,我们整一个MVP就完成了。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.himarking.myapplication.MainActivity">
    
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_marginTop="100dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:layout_margin="10dp"
                android:layout_marginTop="200dp"
                android:id="@+id/et_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <EditText
                android:layout_margin="10dp"
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_marginTop="200dp"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:id="@+id/bt_login"
                android:text="登陆"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/bt_clear"
                android:text="清空"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    
    </LinearLayout>
  • 相关阅读:
    (爱加密系列教程十四)利用无效字节码指令引发逆向工具崩溃(一)
    (爱加密系列教程十三) 如何防止工具(八门神器)进行内存修改
    (爱加密系列教程十二) 如何防止jd-gui查看代码
    (爱加密系列教程十一) 关于某银行apk的安全分析
    超简教程:Xgboost在Window上的安装(免编译)
    自己动手实现机器学习算法:神经网络(附源代码)
    Spark 源码解析:TaskScheduler的任务提交和task最佳位置算法
    Spark 源码解析 : DAGScheduler中的DAG划分与提交
    16.Spark Streaming源码解读之数据清理机制解析
    14:Spark Streaming源码解读之State管理之updateStateByKey和mapWithState解密
  • 原文地址:https://www.cnblogs.com/linfenghp/p/9707248.html
Copyright © 2011-2022 走看看