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ù)庫(kù)或聯(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)文章
Flutter?Widget之NavigationBar使用詳解
這篇文章主要為大家介紹了Flutter?Widget之NavigationBar使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細(xì)介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
基于Alarmmanager實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了基于Alarmmanager實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
教你制作Android中炫酷的ViewPagerIndicator(不僅仿MIUI)
ViewPagerIndicator作為一款分頁(yè)指標(biāo)小部件兼容ViewPager,封裝上做得非常不錯(cuò),目前已為眾多知名應(yīng)用所使用。今天給你大家分享一個(gè)炫酷的效果,有需要的可以參考學(xué)習(xí)。2016-08-08
Android實(shí)現(xiàn)底部彈出PopupWindow背景逐漸變暗效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部彈出PopupWindow背景逐漸變暗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android使用第三方庫(kù)實(shí)現(xiàn)日期選擇器
這篇文章主要為大家詳細(xì)介紹了Android使用第三方庫(kù)實(shí)現(xiàn)日期選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
解決Eclipse啟動(dòng)出錯(cuò):Failed to create the Java Virtual Machine
這篇文章主要介紹了解決Eclipse啟動(dòng)出錯(cuò):Failed to create the Java Virtual Machine的相關(guān)資料,這里說明出錯(cuò)原因及查找錯(cuò)誤和解決辦法,需要的朋友可以參考下2017-07-07
Android實(shí)現(xiàn)底部彈出的對(duì)話框功能
這篇文章主要介紹了Android實(shí)現(xiàn)底部彈出的對(duì)話框功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

