添加权限(有的权限我们可能不需要用)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
先说一下布局吧
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.example.administrator.fuxi_04.View.MainActivity">
<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_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button"
android:onClick="LoginClick"
android:text="登录" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_marginLeft="29dp"
android:layout_marginStart="29dp"
android:layout_toEndOf="@+id/align"
android:layout_toRightOf="@+id/align"
android:onClick="ClearClick"
android:text="重置" />
<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="注册"
android:id="@+id/button2" />
</RelativeLayout>
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.example.administrator.fuxi_04.View.RegisterActivity">
<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="确定" />
<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="重置" />
</RelativeLayout>
Bean
User.java
package com.example.administrator.fuxi_04.Bean;
/**
* Created by Administrator on 2018/4/29/029.
*/
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;
}
}
Model
LoginModel.java
package com.example.administrator.fuxi_04.Model;
import android.content.Context;
import com.example.administrator.fuxi_04.Bean.User;
/**
* Created by Administrator on 2018/4/29/029.
*/
public interface LoginModel {
void login(String username, String password, OnLoginListener onLoginListener, Context context);
interface OnLoginListener {
void loginSuccess(User user);
void loginFailed(String s);
}
}
LoginModelImpl.java
package com.example.administrator.fuxi_04.Model;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.administrator.fuxi_04.Bean.User;
/**
* Created by Administrator on 2018/4/29/029.
*/
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.java
package com.example.administrator.fuxi_04.Model;
import android.content.Context;
import com.example.administrator.fuxi_04.Bean.User;
/**
* Created by Administrator on 2018/4/29/029.
*/
public interface RegisterModel {
void register(String username, String password, OnRegisterListener onRegisterListener, Context context);
interface OnRegisterListener {
void registerSuccess(User user);
void registerFailed(String s);
}
}
RegisterModelImpl.java
package com.example.administrator.fuxi_04.Model;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.administrator.fuxi_04.Bean.User;
/**
* Created by Administrator on 2018/4/29/029.
*/
public class RegisterModelImpl implements RegisterModel {
@Override
public void register(final String username, final String password, final RegisterModel.OnRegisterListener onRegisterListener, Context context) {
@SuppressLint("WrongConstant") 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();
}
}
Presenter
LoginPresenter.java
package com.example.administrator.fuxi_04.Personter;
import android.os.Handler;
import com.example.administrator.fuxi_04.Bean.User;
import com.example.administrator.fuxi_04.Model.LoginModel;
import com.example.administrator.fuxi_04.Model.LoginModelImpl;
import com.example.administrator.fuxi_04.View.LoginView;
/**
* Created by Administrator on 2018/4/29/029.
*/
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.java
package com.example.administrator.fuxi_04.Personter;
import android.os.Handler;
import com.example.administrator.fuxi_04.Bean.User;
import com.example.administrator.fuxi_04.Model.RegisterModel;
import com.example.administrator.fuxi_04.Model.RegisterModelImpl;
import com.example.administrator.fuxi_04.View.RegisterView;
/**
* Created by Administrator on 2018/4/29/029.
*/
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.java
package com.example.administrator.fuxi_04.View;
import android.content.Context;
import com.example.administrator.fuxi_04.Bean.User;
/**
* Created by Administrator on 2018/4/29/029.
*/
public interface LoginView {
//得到用户填写的信息
String getUsername();
String getPassword();
Context context();
//显示和隐藏登录ProgressBar
void showLoading();
void hideLoading();
//登录成功或失败后,返回信息的方法
void showSuccessMsg(User user);
void showFailedMsg(String s);
//清楚数据
void clearEditText();
}
MainActivity.java
package com.example.administrator.fuxi_04.View;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.administrator.fuxi_04.Bean.User;
import com.example.administrator.fuxi_04.Personter.LoginPresenter;
import com.example.administrator.fuxi_04.R;
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();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
@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.java
package com.example.administrator.fuxi_04.View;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.administrator.fuxi_04.Bean.User;
import com.example.administrator.fuxi_04.Personter.RegisterPresenter;
import com.example.administrator.fuxi_04.R;
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();
}
}
RegisterView.java
package com.example.administrator.fuxi_04.View;
import android.content.Context;
import com.example.administrator.fuxi_04.Bean.User;
/**
* Created by Administrator on 2018/4/29/029.
*/
public interface RegisterView {
//得到用户填写的信息
String getUsername();
String getPassword();
Context context();
//显示和隐藏登录ProgressBar
void showLoading();
void hideLoading();
//注册成功或失败后,返回信息的方法
void showSuccessMsg(User user);
void showFailedMsg(String s);
//清除数据
void clearEditText();
}
如有问题:请参考原博客https://blog.csdn.net/BOKE1/article/details/78243419