Android封装MVP实现登录注册功能
作者:赢le 时间:2021-06-14 20:45:08
本文实例为大家分享了Android封装MVP实现登录注册功能,供大家参考,具体内容如下
model包:
import com.bwei.mvps.bean.UserBean;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/9/1 16:00
*/
public interface IUserModel {
void setFirstName(String firstName);
void setLastName(String lastName);
String getFirstName();
String getLastName();
//根据id获取对象
UserBean load(int id);
}
import android.util.Log;
import com.bwei.mvps.bean.UserBean;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/9/1 16:04
*/
public class UserModel implements IUserModel {
@Override
public void setFirstName(String firstName) {
Log.i("xxx", firstName);
}
@Override
public void setLastName(String lastName) {
Log.i("xxx", lastName);
}
@Override
public String getFirstName() {
return null;
}
@Override
public String getLastName() {
return null;
}
@Override
public UserBean load(int id) {
//查询数据库或联网获取数据
Log.i("fff", id + "");
return new UserBean("张", "三");
}
}
View包
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/9/1 15:53
*/
public interface UserView {
void setFirstName(String firstName);
void setLastName(String lastName);
int getId();
String getFirstName();
String getLastName();
}
presenter包:
import android.util.Log;
import com.bwei.mvps.MainActivity;
import com.bwei.mvps.bean.UserBean;
import com.bwei.mvps.model.IUserModel;
import com.bwei.mvps.model.UserModel;
import com.bwei.mvps.view.UserView;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/9/1 16:05
*/
public class UserPresenter {
private UserView userview;
private final IUserModel iUserModel;
public UserPresenter(UserView userview) {
this.userview = userview;
iUserModel = new UserModel();
}
//保存数据
public void saveUser(int id, String firstName, String lastName) {
UserBean userBean = iUserModel.load(id);
Log.i("sss", "id:" + id + ",firstName:" + firstName + ",lastName:" + lastName);
}
//查询数据
public void find(int id) {
UserBean userBean = iUserModel.load(id);
String firstName = userBean.getFirstName();
String lastName = userBean.getLastName();
userview.setFirstName(firstName);
userview.setLastName(lastName);
Log.i("aaa", "id:" + id + ",firstName:" + firstName + ",lastName:" + lastName);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="ID"/>
<EditText
android:id="@+id/et_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="FirstName"/>
<EditText
android:id="@+id/et_first_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="LastName"/>
<EditText
android:id="@+id/et_last_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册"/>
<Button
android:id="@+id/bt_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录"/>
</LinearLayout>
</LinearLayout>
Mactivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.bwei.mvps.presenter.UserPresenter;
import com.bwei.mvps.view.UserView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, UserView {
private EditText et_id;
private EditText et_first_name;
private EditText et_last_name;
private Button bt_login;
private Button bt_register;
private UserPresenter userPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
et_id = (EditText) findViewById(R.id.et_id);
et_first_name = (EditText) findViewById(R.id.et_first_name);
et_last_name = (EditText) findViewById(R.id.et_last_name);
bt_login = (Button) findViewById(R.id.bt_login);
bt_register = (Button) findViewById(R.id.bt_register);
bt_login.setOnClickListener(this);
bt_register.setOnClickListener(this);
//声明UserPresenter
userPresenter = new UserPresenter(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_register://保存数据
userPresenter.saveUser(getId(), getFirstName(), getLastName());
break;
case R.id.bt_login:
userPresenter.find(getId());
break;
}
}
@Override
public void setFirstName(String firstName) {
et_first_name.setText(firstName);
}
@Override
public void setLastName(String lastName) {
et_last_name.setText(lastName);
}
@Override
public int getId() {
return new Integer(et_id.getText().toString());
}
@Override
public String getFirstName() {
return et_first_name.getText().toString();
}
@Override
public String getLastName() {
return et_last_name.getText().toString();
}
}
来源:http://blog.csdn.net/qq_38259132/article/details/78243760
标签:Android,MVP,登录,注册
0
投稿
猜你喜欢
Struts2学习笔记(8)-Result常用类型
2023-06-05 11:10:19
spring中的BeanFactory与FactoryBean的讲解
2023-03-13 11:18:09
详解Android中提示对话框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)
2023-05-10 19:27:43
Java设计模式之访问者模式使用场景及代码示例
2021-06-27 13:34:25
java 生成有序账号的实现方法
2023-08-12 03:28:01
springboot 定时任务@Scheduled实现解析
2023-11-10 16:18:24
详解Java中多进程编程的实现
2021-12-22 01:46:06
如何使用IDEA的groovy脚本文件生成带JPA注解的实体类(图文详解)
2022-02-04 02:42:04
C#中Hashtable和Dictionary的区别与用法示例
2023-10-05 03:22:59
如何利用Spring MVC实现RESTful风格
2021-06-06 02:02:13
Java利用cors实现跨域请求实例
2023-02-24 14:57:35
利用Java连接Hadoop进行编程
2022-11-12 09:02:12
23种设计模式(1) java单例模式
2021-08-28 21:56:28
浅谈JAVA设计模式之享元模式
2021-09-22 10:12:06
Android实现Service获取当前位置(GPS+基站)的方法
2023-04-03 16:28:28
利用Spring boot+LogBack+MDC实现链路追踪
2023-10-03 16:02:53
Android开发TextView内的文字实现自动换行
2023-06-21 12:27:48
java compare compareTo方法区别详解
2022-06-26 08:13:55
Windows Zookeeper安装过程及启动图解
2021-09-15 11:07:55
SpringBoot解决Required String parameter xxx is not present问题
2021-08-08 12:53:15