您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android中如何利用MVP實現登錄注冊功能的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Android中如何利用MVP實現登錄注冊功能文章都會有所收獲,下面我們一起來看看吧。
model包:
import com.bwei.mvps.bean.UserBean; 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; 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包
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; 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實現登錄注冊功能”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Android中如何利用MVP實現登錄注冊功能”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。