登陆界面
程序员文章站
2022-04-18 17:10:26
...
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, UserView {
private EditText mUserName, mUserPass;
private CheckBox mCheckBox;
private SharedPreferences sp;
private UserPresenterIml userPresenterIml;
@Override
protected void onCreate(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 = (EditText) findViewById(R.id.user_name);
mUserPass = (EditText) findViewById(R.id.user_pass);
mCheckBox = (CheckBox) findViewById(R.id.checkbox);
// getSharedPreferences 存值
sp = getSharedPreferences("user", MODE_PRIVATE);
// newP层,调用登录方法
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)) {
mUserPass.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() {
// 获取输入的账号密码
String userName = mUserName.getText().toString().trim();
String userPass = mUserPass.getText().toString().trim();
// 校验
if (TextUtils.isEmpty(userName)){
toast("请输入您的账号");
return;
}
if (TextUtils.isEmpty(userPass)){
toast("请输入您的密码");
return;
}
// 给记住密码默认值
boolean isCheckBox = mCheckBox.isChecked();
// 进行判断
if (isCheckBox){// 保存账号密码
sp.edit().putString("user_name",userName)
.putString("user_pass",userPass).commit();
}else {// 否者清空
sp.edit().putString("user_name","")
.putString("user_pass","").commit();
}
// 判断无误后走登录方法
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();
// 跳转展示页面
startActivity(new Intent(LoginActivity.this,UserInFoActivity.class));
}
// 登录失败
@Override
public void fail(String error) {
// 吐司登录失败
toast(error);
}
}