美文网首页
2018-03-19 Android MVP模式初识

2018-03-19 Android MVP模式初识

作者: KryNa | 来源:发表于2018-03-19 16:46 被阅读0次

使用简单登陆功能进行说明:

MVP:model、view、presenter

1.目录结构

2.MainActivity.java, 重写接口中的方法,与Presenter连接,通过Presenter展示页面数据

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ProgressBar;

import android.widget.Toast;

import com.example.alva.mvptest.R;

import com.example.alva.mvptest.presenter.ILoginPresenter;

import com.example.alva.mvptest.presenter.ILoginPresenterImpl;

import butterknife.BindView;

import butterknife.ButterKnife;

public class MainActivityextends AppCompatActivityimplements ILoginView, View.OnClickListener{

        @BindView(R.id.ed_username)

        EditTexted_username;

        @BindView(R.id.ed_password)

        EditText  ed_password;

        @BindView(R.id.bt_login)

        Button  bt_login;

        @BindView(R.id.bt_clear)

        Button  bt_clear;

        @BindView(R.id.pb_login)

        ProgressBar  pb_login;

        ILoginPresenter  iLoginPresenter;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);

                ButterKnife.bind(this);

                bt_clear.setOnClickListener(this);

                bt_login.setOnClickListener(this);

                iLoginPresenter =new ILoginPresenterImpl(this);//创建Presenter实例

                iLoginPresenter.setProgressBarVisiblity(View.INVISIBLE);

            }

        public void onClick(View view){

                switch (view.getId()){

                        case R.id.bt_login:

                                 iLoginPresenter.setProgressBarVisiblity(View.VISIBLE);

                                bt_login.setEnabled(false);

                                bt_clear.setEnabled(false);

                                iLoginPresenter.doLogin(ed_username.getText().toString(),ed_password.getText().toString());

                                break;

                        case R.id.bt_clear:

                                  iLoginPresenter.clear();

                                 break;

                        }

            }

            @Override

            public void onClearText() {

                        ed_password.setText("");

                        ed_username.setText("");

             }

            @Override

             public void onLoginResult(Boolean result, int code) {

                        iLoginPresenter.setProgressBarVisiblity(View.INVISIBLE);

                        bt_login.setEnabled(true);

                        bt_clear.setEnabled(true);

                        if(result){

                                Log.i("login", "onLoginResult: success****");

                                Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();

                        }else{

                                Log.i("login", "onLoginResult: fail****");

                                Toast.makeText(MainActivity.this,"登陆失败,code="+code,Toast.LENGTH_SHORT).show();

                        }

            }

            @Override

            public void onSetProgressBarVisibility(int visibility) {

                        pb_login.setVisibility(visibility);

            }

}

3.Presenter层,调用View接口中的方法(接口中的方法已经被MainActivity重写)并且与Model层相连接

Presenter层接口 Presennter接口实现类

P层调用Model中的方法,通过接口接收M层传来的数据,并通过View将数据返回到View层

可以看到在实现类中,重写的方法都是调用View层接口中的方法(PS:在View层中调用Presenter层的方法,Presenter层重写的方法调用的是在MainActivity.java重写的方法)

之后,创建了Model实例,调用了Model层的方法checkUserValidity(username,password)

4.Model层

若需要将Model中获取的数据返回到P层,则需要通过Model的监听接口返回到Presenter层

model层接口 Model层实现类 调用接口返回数据到P层

参照:

http://blog.csdn.net/limonzet/article/details/53328315

http://blog.csdn.net/lmj623565791/article/details/46596109

相关文章

网友评论

      本文标题:2018-03-19 Android MVP模式初识

      本文链接:https://www.haomeiwen.com/subject/zcupqftx.html