android+使用mvp做一个简单的登录

model包内

IUserMode接口类

package model;

import bawei.com.denglu.User;

/**
 * Created by Administrator on 2017/11/1.
 */

public interface IUserMode {
    /**
     * 登录
     *
     * @param user
     * @return 约定返回"true"为登录成功,其他为登录失败的错误信息
     */
    String login(User user);
}
UserMode类(实现接口)
package model;

import bawei.com.denglu.User;

/**
 * Created by Administrator on 2017/11/1.
 */

public class UserMode implements IUserMode {
    @Override
    public String login(User user) {
        boolean networkError = false; //网络是否异常

        try {
            Thread.sleep(3000);//模拟网络连接
            if (networkError) {
                return "网络异常";
            } else if ("ethanco".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
                return "true";
            } else {
                return "账号或密码错误";
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
view包下
IUserView接口类
package view;

/**
 * Created by Administrator on 2017/11/1.
 */

public interface IUserView {
    /**
     * 登录成功
     */
    void onLoginSuccess();

    /**
     * 登录失败
     *
     * @param error
     */
    void onLoginFailed(String error);
}
presenter包下
 
UserPresenter类
package presenter;

import android.os.Handler;
import android.os.Looper;

import bawei.com.denglu.User;
import model.UserMode;
import view.IUserView;

/**
 * Created by Administrator on 2017/11/1.
 */

public class UserPresenter {
    private final IUserView userView;
    private final UserMode userMode;

    public UserPresenter(IUserView userView) {
        this.userView = userView;
        this.userMode = new UserMode();
    }

    /**
     * 登录
     *
     * @param user
     */
    public void login(final User user) {
        new Thread() {
            @Override
            public void run() {
                final String res = userMode.login(user);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if ("true".equals(res)) {
                            userView.onLoginSuccess();
                        } else {
                            userView.onLoginFailed(res);
                        }
                    }
                });
            }
        }.start();
    }
}
User
package bawei.com.denglu;

/**
 * Created by Administrator on 2017/11/1.
 */
public class User {
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private String username;
    private String password;
}
MainActivity
package bawei.com.denglu;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import presenter.UserPresenter;
import view.IUserView;

public class MainActivity extends AppCompatActivity implements IUserView, View.OnClickListener {

    private UserPresenter userPresenter;
    private EditText etUserName;
    private EditText etPassword;
    private ProgressDialog loginProgreess;

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

        etUserName = (EditText) findViewById(R.id.et_userName);
        etPassword = (EditText) findViewById(R.id.et_password);
        findViewById(R.id.btn_login).setOnClickListener(this);
        userPresenter = new UserPresenter(this);
    }

    @Override
    public void onLoginSuccess() {
        loginProgreess.dismiss();
        Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoginFailed(String error) {
        loginProgreess.dismiss();
        Toast.makeText(getApplicationContext(), "登录失败:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        String username = etUserName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(getApplicationContext(), "账号或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        loginProgreess = ProgressDialog.show(this, "登录", "正在登录...");
        userPresenter.login(new User(username, password));
    }
}
activity_main类
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"

    tools:context="bawei.com.denglu.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_userName"
        android:text="账号"
        android:layout_margin="15dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_password"
        android:text="密码"
        android:layout_below="@+id/et_userName"
        android:layout_margin="15dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/btn_login"
        android:text="登录"

        />

</RelativeLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值