Android MVP Contract

阎弘雅
2023-12-01

        MVP简单登陆 Demo 

1.创建Presenter View 基类

public interface BasePresenter {

    void start();
}
public interface BaseView<T> {
//这里使用范型
    void setPresneter(T presneter);
}

 2.创建集合接口LoginConteact 所有View,Presenter 接口都写在这里

public interface LoginContract {

    interface View extends BaseView<Presenter> {
        String getUserName();

        String getPassWord();

        void LoginSuccess();

        void LoginFail(String error);

        void resetEditView();
    }

    interface Presenter extends BasePresenter {
        void login();

        void reset();
    }

3.写LoginPresenter 这里主要是业务逻辑

public class LoginPresenter implements LoginContract.Presenter {

    private Context mContext;
    private LoginContract.View mLoginView;

    public LoginPresenter(Context context, LoginContract.View loginView) {
        this.mLoginView = loginView;
        this.mContext = context;
        mLoginView.setPresneter(this);
    }

    @Override
    public void login() {
        attemptLogin();
    }

    @Override
    public void reset() {
     mLoginView.resetEditView();
    }

    @Override
    public void start() {
    }


    private void attemptLogin() {
        String userName = mLoginView.getUserName();
        String passWord = mLoginView.getPassWord();

        try {
//            模拟网络耗时操作
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (userName.equals("123") && passWord.equals("456")) {
            mLoginView.LoginSuccess();
        } else {
            mLoginView.LoginFail("密码错误");
        }
    }
}

4.Activity调用

public class MainActivity extends AppCompatActivity {

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

        LoginFragment loginFragment = (LoginFragment) getSupportFragmentManager().
                findFragmentById(R.id.contentFrame);

        if (loginFragment == null) {
            loginFragment = LoginFragment.newInstance("LOGIN_FRAGMENT");
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.contentFrame, loginFragment);
        transaction.commit();

        new LoginPresenter(this, loginFragment);
    }

 

5.Fragment

public class LoginFragment extends Fragment implements LoginContract.View {


    private LoginContract.Presenter mPresenter;
    private Button mLoginBtn, mResetBtn;
    private TextView mUserNameEdit, mPassEdit;


    public static LoginFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString("fragment_id", id);
        LoginFragment loginFragment = new LoginFragment();
        loginFragment.setArguments(args);
        return loginFragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_login, container, false);
        initVew(root);
        return root;
    }

    private void initVew(View view) {

        mUserNameEdit = view.findViewById(R.id.editT_username);
        mPassEdit = view.findViewById(R.id.editT_password);
        mLoginBtn = view.findViewById(R.id.btn_login);
        mResetBtn = view.findViewById(R.id.btn_reset);

        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.login();
            }
        });

        mResetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.reset();
            }
        });
    }

    @Override
    public String getUserName() {
        return mUserNameEdit.getText().toString();
    }

    @Override
    public String getPassWord() {
        return mPassEdit.getText().toString();
    }

    @Override
    public void LoginSuccess() {
        Toast.makeText(getContext(), "登陆成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void LoginFail(String error) {
        Toast.makeText(getContext(), "失败了" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void resetEditView() {
        mUserNameEdit.setText("");
        mPassEdit.setText("");
    }

    @Override
    public void setPresneter(LoginContract.Presenter presneter) {
        this.mPresenter = presneter;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter = null;
    }
}

6.布局

//activity_main
<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"
    android:padding="50dp">

    <FrameLayout
        android:id="@+id/contentFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        tools:context=".login.LoginActivity">

    </FrameLayout>

</LinearLayout>

 

//fragment_login

<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:gravity="center_horizontal"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editT_username"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <EditText
        android:id="@+id/editT_password"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="登 录" />

    <Button
        android:id="@+id/btn_reset"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="重 置" />
</LinearLayout>

 

Demo 链接地址 https://download.csdn.net/download/csdndouniwan/11493702

 类似资料:

相关阅读

相关文章

相关问答