Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能
本文實(shí)例為大家分享了Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能,供大家參考,具體內(nèi)容如下
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();
//根據(jù)id獲取對(duì)象
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) {
//查詢數(shù)據(jù)庫或聯(lián)網(wǎng)獲取數(shù)據(jù)
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();
}
//保存數(shù)據(jù)
public void saveUser(int id, String firstName, String lastName) {
UserBean userBean = iUserModel.load(id);
Log.i("sss", "id:" + id + ",firstName:" + firstName + ",lastName:" + lastName);
}
//查詢數(shù)據(jù)
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="注冊(cè)"/> <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://保存數(shù)據(jù)
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();
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義TextView實(shí)現(xiàn)drawableLeft內(nèi)容居中
這篇文章主要介紹了Android自定義TextView實(shí)現(xiàn)drawableLeft內(nèi)容居中的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子,例子邏輯清晰,但相對(duì)簡(jiǎn)單了些,需要的朋友可以參考下2014-07-07
android實(shí)現(xiàn)狀態(tài)欄添加圖標(biāo)的函數(shù)實(shí)例
這篇文章主要介紹了android實(shí)現(xiàn)狀態(tài)欄添加圖標(biāo)的函數(shù),較為詳細(xì)的分析了Android狀態(tài)欄添加及刪除圖標(biāo)的具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android 點(diǎn)擊屏幕空白處收起輸入法軟鍵盤(手動(dòng)打開)
很多時(shí)候,我們?cè)谑褂脩?yīng)用時(shí),會(huì)出現(xiàn)輸入法軟鍵盤彈出的問題,通常情況下,我們默認(rèn)會(huì)使用戶點(diǎn)擊返回鍵或者下一步對(duì)軟鍵盤進(jìn)行隱藏。為了更好的體驗(yàn),我們可以實(shí)現(xiàn)當(dāng)用戶使用完畢軟鍵盤時(shí)。點(diǎn)擊屏幕空白即可實(shí)現(xiàn)收起輸入法軟鍵盤2016-12-12
Android 實(shí)現(xiàn)定時(shí)任務(wù)的過程詳解
這篇文章主要介紹了Android 定時(shí)任務(wù)過程詳解的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)三段式滑動(dòng)效果
最近發(fā)現(xiàn)很多app都使用了三段式滑動(dòng),比如說高德的首頁和某寶等物流信息都是使用的三段式滑動(dòng)方式,谷歌其實(shí)給了我們很好的2段式滑動(dòng),就是BottomSheet,所以這次我也是在這個(gè)原理基礎(chǔ)上做了一個(gè)小小的修改來實(shí)現(xiàn)我們今天想要的效果。2021-06-06
android使用NotificationListenerService監(jiān)聽通知欄消息
本篇文章主要介紹了android使用NotificationListenerService監(jiān)聽通知欄消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android實(shí)現(xiàn)紅包雨動(dòng)畫效果
本篇文章主要介紹了Android實(shí)現(xiàn)紅包雨動(dòng)畫效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07

