LoginActivity
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, UserView {
private EditText mUserName, mUserPass;
private CheckBox mCheckBox;
private SharedPreferences sp;
private UserPresenterIml userPresenterIml;
private String userName, userPass;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.tv_register).setOnClickListener(this);
findViewById(R.id.btn_login).setOnClickListener(this);
mUserName = findViewById(R.id.user_name);
mUserPass = findViewById(R.id.user_pass);
mCheckBox = findViewById(R.id.checkbox);
sp = getSharedPreferences("user", MODE_PRIVATE);
userPresenterIml = new UserPresenterIml(new UserModelIml(), this);
}
@Override
protected void onResume() {
super.onResume();
String userName = sp.getString("user_name", null);
String userPass = sp.getString("user_pass", null);
if (!TextUtils.isEmpty(userName)){
mUserName.setText(userName);
}
if (!TextUtils.isEmpty(userPass)){
mUserName.setText(userPass);
mCheckBox.setChecked(true);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_register://跳转到注册
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
break;
case R.id.btn_login://点击登录
doLogin();
break;
}
}
//登录
private void doLogin() {
userName = mUserName.getText().toString().trim();
userPass = mUserPass.getText().toString().trim();
if (TextUtils.isEmpty(userName)) {
toast("请输入您的账号");
return;
}
if (TextUtils.isEmpty(userName)) {
toast("请输入您的密码");
return;
}
//走登录
userPresenterIml.doLogin(userName, userPass);
}
private void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void success(String data) {
UserInFoBean bean = new Gson().fromJson(data, UserInFoBean.class);
sp.edit().putString("user_name", bean.getResult().getPhone())
.putString("user_nickName", bean.getResult().getNickName())
.putString("user_pictrue", bean.getResult().getHeadPic())
.commit();
boolean isChekBox = mCheckBox.isChecked();
if (isChekBox) {
sp.edit().putString("user_name", userName)
.putString("user_pass", userPass).commit();
} else {
sp.edit().putString("user_name", "")
.putString("user_pass", "").commit();
}
//跳到用户信息展示页面
startActivity(new Intent(LoginActivity.this, UserInFoActivity.class));
}
@Override
public void fail(String error) {
toast(error);
}
}
注册RegisterActivity
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, UserView {
private EditText mUserName, mUserPass, mUserPass2;
private UserPresenterIml mUserPresenterIml;
private SharedPreferences sp;
private String userName;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
findViewById(R.id.btn_register).setOnClickListener(this);
findViewById(R.id.tv_login).setOnClickListener(this);
mUserName = findViewById(R.id.user_name);
mUserPass = findViewById(R.id.user_pass);
mUserPass2 = findViewById(R.id.user_pass2);
mUserPresenterIml = new UserPresenterIml(new UserModelIml(), this);
sp = getSharedPreferences("user", MODE_PRIVATE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_register://点击注册
doRegister();
break;
case R.id.tv_login://已有账号,立即注册
finish();
break;
}
}
//注册
private void doRegister() {
userName = mUserName.getText().toString().trim();
String userPass = mUserPass.getText().toString().trim();
String userPass2 = mUserPass2.getText().toString().trim();
if (TextUtils.isEmpty(userName)) {
toast("请输入你的账号");
return;
}
if (TextUtils.isEmpty(userPass)) {
toast("请输入你的密码");
return;
}
if (TextUtils.isEmpty(userPass2)) {
toast("请输确认的密码");
return;
}
if (!userPass.equals(userPass2)) {
toast("请检查您两次密码是否一致");
return;
}
//走注册
mUserPresenterIml.doRegister(userName,userPass);
}
private void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void success(String data) {
UserBean bean = new Gson().fromJson(data, UserBean.class);
toast(bean.getMessage());
sp.edit().putString("user_name", userName).commit();
if (bean.getStatus().equals("0000")) {
finish();
}
}
@Override
public void fail(String error) {
toast(error);
}
@Override
protected void onDestroy() {
super.onDestroy();
mUserPresenterIml.destory();
}
}
登录成功展示图片UserInFoActivity
public class UserInFoActivity extends AppCompatActivity {
private SharedPreferences sp;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_in_fo);
sp = getSharedPreferences("user", MODE_PRIVATE);
String userName = sp.getString("user_name",null);
String userNickName = sp.getString("user_nickName",null);
String userPictrue = sp.getString("user_pictrue",null);
ImageView imageView = findViewById(R.id.image_pictrue);
TextView textView = findViewById(R.id.tv_nickname);
Glide.with(this).load(userPictrue).into(imageView);
textView.setText(userNickName);
}
}