欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android封装MVP实现登录注册功能

程序员文章站 2023-08-12 13:16:17
本文实例为大家分享了android封装mvp实现登录注册功能,供大家参考,具体内容如下 model包: import com.bwei.mvps.bean.us...

本文实例为大家分享了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();
 }
}

Android封装MVP实现登录注册功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。