MVP实现登录注册功能
来源:互联网 发布:mac ai抠图教程视频 编辑:程序博客网 时间:2024/05/18 12:30
Model
bean
public class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}LoginModel
public interface LoginModel { void login(String username, String password, OnLoginListener onLoginListener, Context context); interface OnLoginListener { void loginSuccess(User user); void loginFailed(String s); }}LoginModelImpl
public class LoginModelImpl implements LoginModel { @Override public void login(final String username, final String password, final OnLoginListener onLoginListener, Context context) { /** * * 模拟子线程,执行耗时操作 */ final SharedPreferences user = context.getSharedPreferences("user", Context.MODE_APPEND); new Thread() { @Override public void run() { super.run(); try { Thread.sleep(3000); if (username.isEmpty()||password.isEmpty()){ onLoginListener.loginFailed("Incorrect username or password."); return; } if (username.equals(user.getString("name","")) && password.equals(user.getString("pwd",""))) { onLoginListener.loginSuccess(new User(username,password)); } else { onLoginListener.loginFailed("Incorrect username or password."); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }}
RegisterModel
public interface RegisterModel { void register(String username, String password, OnRegisterListener onRegisterListener, Context context); interface OnRegisterListener { void registerSuccess(User user); void registerFailed(String s); }}RegisterModelImpl
public class RegisterModelImpl implements RegisterModel { @Override public void register(final String username, final String password, final OnRegisterListener onRegisterListener, Context context) { final SharedPreferences user = context.getSharedPreferences("user", Context.MODE_APPEND); new Thread() { @Override public void run() { super.run(); try { Thread.sleep(3000); if (username.isEmpty() || password.isEmpty()) { onRegisterListener.registerFailed("用户或密码不能为空"); return; } if (username.equals(user.getString("name", ""))) { onRegisterListener.registerFailed("用户已存在"); } else { onRegisterListener.registerSuccess(new User(username, password)); SharedPreferences.Editor edit = user.edit(); edit.putString("name", username); edit.putString("pwd", password); edit.commit(); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }}
Personter
LoginPresenter
public class LoginPresenter { private LoginView loginView; private LoginModel loginModel; private Handler mHandler; public LoginPresenter(LoginView loginView) { this.loginView = loginView; loginModel = new LoginModelImpl(); mHandler = new Handler(); } public void login() { loginView.showLoading(); loginModel.login(loginView.getUsername(), loginView.getPassword(), new LoginModel.OnLoginListener() { @Override public void loginSuccess(final User user) { //模拟登录成功后,返回信息到Activity,吐出成功信息 mHandler.post(new Runnable() { @Override public void run() { loginView.showSuccessMsg(user); loginView.hideLoading(); } }); } @Override public void loginFailed(final String s) { //模拟登录失败后,返回信息,吐出错误信息 mHandler.post(new Runnable() { @Override public void run() { loginView.showFailedMsg(s); loginView.hideLoading(); } }); } },loginView.context()); } public void clear(){ loginView.clearEditText(); }}
RegisterPresenter
public class RegisterPresenter { private RegisterView registerView; private RegisterModelImpl registerModelImpl; private Handler mHandler; public RegisterPresenter(RegisterView registerView) { this.registerView = registerView; registerModelImpl = new RegisterModelImpl(); mHandler = new Handler(); } public void register() { registerView.showLoading(); registerModelImpl.register(registerView.getUsername(), registerView.getPassword(), new RegisterModel.OnRegisterListener() { @Override public void registerSuccess(final User user) { //模拟注册成功后,返回信息到Activity,吐出成功信息 mHandler.post(new Runnable() { @Override public void run() { registerView.showSuccessMsg(user); registerView.hideLoading(); } }); } @Override public void registerFailed(final String s) { //模拟注册失败后,返回信息,吐出错误信息 mHandler.post(new Runnable() { @Override public void run() { registerView.showFailedMsg(s); registerView.hideLoading(); } }); } }, registerView.context()); } public void clear() { registerView.clearEditText(); }}
View
LoginView
public interface LoginView { //得到用户填写的信息 String getUsername(); String getPassword(); Context context(); //显示和隐藏登录ProgressBar void showLoading(); void hideLoading(); //登录成功或失败后,返回信息的方法 void showSuccessMsg(User user); void showFailedMsg(String s); //清楚数据 void clearEditText();}
RegisterView
public interface RegisterView { //得到用户填写的信息 String getUsername(); String getPassword(); Context context(); //显示和隐藏登录ProgressBar void showLoading(); void hideLoading(); //注册成功或失败后,返回信息的方法 void showSuccessMsg(User user); void showFailedMsg(String s); //清除数据 void clearEditText();}
MainActivity
public class MainActivity extends AppCompatActivity implements LoginView { private EditText et_userName; private EditText et_password; private ProgressBar progressBar; private LoginPresenter loginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); loginPresenter = new LoginPresenter(this); } private void initview() { et_userName = (EditText) findViewById(R.id.main_et_username); et_password = (EditText) findViewById(R.id.main_et_password); progressBar = (ProgressBar) findViewById(R.id.main_progressBar); } //点击登录 public void LoginClick(View view) { loginPresenter.login(); } //点击清除 public void ClearClick(View view) { loginPresenter.clear(); } @Override public String getUsername() { return et_userName.getText().toString(); } @Override public String getPassword() { return et_password.getText().toString(); } @Override public Context context() { return this; } @Override public void showLoading() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideLoading() { progressBar.setVisibility(View.GONE); } @Override public void showSuccessMsg(User user) { Toast.makeText(MainActivity.this, "User " + user.getUsername() + " Login Sccess!", Toast.LENGTH_SHORT).show(); } @Override public void showFailedMsg(String s) { Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } @Override public void clearEditText() { et_userName.setText(""); et_password.setText(""); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } public void Register(View view) { startActivity(new Intent(this,RegisterActivity.class)); }}
RegisterActivity
public class RegisterActivity extends AppCompatActivity implements RegisterView { private ImageView align; private ProgressBar progressBar; private EditText et_password; private EditText et_userName; private RegisterPresenter presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); align = (ImageView) findViewById(R.id.align); progressBar = (ProgressBar) findViewById(R.id.main_progressBar); et_password = (EditText) findViewById(R.id.main_et_password); et_userName = (EditText) findViewById(R.id.main_et_username); presenter = new RegisterPresenter(this); } @Override public String getUsername() { return et_userName.getText().toString(); } @Override public String getPassword() { return et_password.getText().toString(); } @Override public Context context() { return this; } @Override public void showLoading() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideLoading() { progressBar.setVisibility(View.GONE); } @Override public void showSuccessMsg(User user) { Toast.makeText(RegisterActivity.this, "User " + user.getUsername() + " Register Sccess!", Toast.LENGTH_SHORT).show(); finish(); } @Override public void showFailedMsg(String s) { Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_SHORT).show(); } @Override public void clearEditText() { et_userName.setText(""); et_password.setText(""); } public void RegisterClick(View view) { presenter.register(); } public void Clear(View view) { presenter.clear(); }}
activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> <EditText android:id="@+id/main_et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:id="@+id/main_et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_et_username" android:layout_marginTop="10dp" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" /> <ProgressBar android:id="@+id/main_progressBar" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:visibility="gone" /> <ImageView android:id="@+id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginRight="30dp" android:layout_marginTop="10dp" android:layout_toLeftOf="@id/align" android:onClick="LoginClick" android:text="Login" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/align" android:onClick="ClearClick" android:text="Clear" /> <Button android:layout_above="@id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_centerHorizontal="true" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:onClick="Register" android:text="注册" /></RelativeLayout>
activity_register
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> <EditText android:id="@+id/main_et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:id="@+id/main_et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_et_username" android:layout_marginTop="10dp" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" /> <ProgressBar android:id="@+id/main_progressBar" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:visibility="gone" /> <ImageView android:id="@+id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginRight="30dp" android:layout_marginTop="10dp" android:layout_toLeftOf="@id/align" android:onClick="RegisterClick" android:text="REGISTER" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/align" android:onClick="Clear" android:text="Clear" /></RelativeLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> <EditText android:id="@+id/main_et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:id="@+id/main_et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_et_username" android:layout_marginTop="10dp" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" /> <ProgressBar android:id="@+id/main_progressBar" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:visibility="gone" /> <ImageView android:id="@+id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginRight="30dp" android:layout_marginTop="10dp" android:layout_toLeftOf="@id/align" android:onClick="LoginClick" android:text="Login" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/align" android:onClick="ClearClick" android:text="Clear" /> <Button android:layout_above="@id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_centerHorizontal="true" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:onClick="Register" android:text="注册" /></RelativeLayout>
activity_register
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> <EditText android:id="@+id/main_et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:id="@+id/main_et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_et_username" android:layout_marginTop="10dp" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" /> <ProgressBar android:id="@+id/main_progressBar" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:visibility="gone" /> <ImageView android:id="@+id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginRight="30dp" android:layout_marginTop="10dp" android:layout_toLeftOf="@id/align" android:onClick="RegisterClick" android:text="REGISTER" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/align" android:onClick="Clear" android:text="Clear" /></RelativeLayout>
阅读全文
0 0
- MVP实现登录注册功能
- 封装MVP 实现登录注册功能
- MVP 实现登录注册
- Mvp实现登录注册
- MVP实现登录注册
- MVP模式实现登录功能
- MVP模式实现登录功能
- Android之SqliteDatabase(MVP模式)实现用户登录注册功能
- ios 实现注册 登录功能
- Android 实现登录注册功能
- php实现登录注册功能
- 表单实现登录注册功能
- Android 实现登录注册功能
- JSP 实现登录注册功能
- 实现登录注册验证功能
- 登录注册mvp模式
- MVP注册登录
- MVP 登录注册
- 从零开始前端学习[40]:css3中的resize,user-select属性,多列布局特性,怪异盒子模型,倒影
- rtklib使用kalman滤波进行高精度相对定位原理
- SwipyRefreshLayout 上拉刷新,下拉加载
- 2017年10月15日训练总结
- Ubuntu 14 安装scrapy
- MVP实现登录注册功能
- 实验4
- Detours的使用
- 枚举类型
- [OpenGL] shadow mapping(实时阴影映射)
- MarkDown语法学习
- Android相关简述题
- 函数指针
- Android之属性动画