net
package com.bwie.xiaqin.denglumvp.net;
import com.bwie.xiaqin.denglumvp.bean.User;
/**
* Created by lenovo on 2018/11/10.
*/
public class UserLoginNet {
//判断信息是否正确
public boolean sendUserLoginInfo(User user){
//对用户输入的信息进行判断是否正确
if ("xia".equals(user.username) && "123".equals(user.password)){
//密码相等就返回true
return true;
}else {
//密码账号不一样
return false;
}
}
}
presenter
package com.bwie.xiaqin.denglumvp.presenter;
import android.os.SystemClock;
import android.text.TextUtils;
import com.bwie.xiaqin.denglumvp.bean.User;
import com.bwie.xiaqin.denglumvp.net.UserLoginNet;
/**
* Created by lenovo on 2018/11/10.
*/
public class Presenter {
public Presenter(PresenterInterface presenterInterface) {
mPresenterInterface = presenterInterface;
}
private User mUser;
public boolean LoginNull(String username, String password){
//创建User
mUser = new User();
mUser.username = username;
mUser.password = password;
if (TextUtils.isEmpty(mUser.username) || TextUtils.isEmpty(mUser.password)){
return false;
}else {
return true;
}
}
public void Login(){
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(2000);
UserLoginNet userLoginNet = new UserLoginNet();
boolean flag = userLoginNet.sendUserLoginInfo(mUser);
if(flag){
mPresenterInterface.success();
}else{
mPresenterInterface.failed();
}
}
}).start();
}
public void detach() {
}
public interface PresenterInterface{
void success();
void failed();
}
private PresenterInterface mPresenterInterface;
public void setPresenterInterface(PresenterInterface mPresenterInterfaces){
mPresenterInterface = mPresenterInterfaces;
}
}
main
package com.bwie.xiaqin.denglumvp;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bwie.xiaqin.denglumvp.presenter.Presenter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,Presenter.PresenterInterface{
private EditText mUsername;
private EditText mPassword;
private Button submit;
private ProgressDialog mProgressDialog;
private Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
presenter = new Presenter(this);
//动态的创建一个进度条的对话框
mProgressDialog = new ProgressDialog(this);
}
private void initView() {
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
submit = (Button) findViewById(R.id.submit);
//点击事件
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:
//获取账号和密码
final String username = mUsername.getText().toString().trim();
final String password = mPassword.getText().toString().trim();
boolean flag = presenter.LoginNull(username, password);
if(flag){
mProgressDialog.show();
presenter.Login();
}else{
//弹出输入不能为空
Toast.makeText(MainActivity.this,"输入的不能为空,么么哒",Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
public void success() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressDialog.dismiss();
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
}
});
}
@Override
public void failed() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressDialog.dismiss();
Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detach();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.bwie.xiaqin.denglumvp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:text="用户名"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"/>
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:text="密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
</LinearLayout>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_gravity="center_horizontal"/>
</LinearLayout>