1、MainActivity
public class MainActivity<T> extends BaseActivity implements IView<T>, View.OnClickListener {
private String url="http://www.xieast.com/api/user/login.php";
private EditText tel;
private EditText pwd;
private CheckBox jizhumima;
private CheckBox zidongdenglu;
private Button tj;
private Button third;
private MyPresenter myPresenter;
private SharedPreferences preferences;
private SharedPreferences.Editor edit;
private String strTel;
private String strPwd;
@Override
protected int getLayout() {
return R.layout.activity_main;
}
@Override
protected void initView() {
tel = findViewById(R.id.tel);
pwd = findViewById(R.id.pwd);
jizhumima = findViewById(R.id.jizhumima);
zidongdenglu = findViewById(R.id.zidongdenglu);
tj = findViewById(R.id.tj);
third = findViewById(R.id.third);
}
@Override
protected void setClick() {
jizhumima.setOnClickListener(this);
zidongdenglu.setOnClickListener(this);
tj.setOnClickListener(this);
third.setOnClickListener(this);
}
@Override
protected void progressLogic() {
preferences = getSharedPreferences("zjh",MODE_PRIVATE);
edit = preferences.edit();
myPresenter = new MyPresenter(this);
boolean isjizhu = preferences.getBoolean("isjizhu", false);
boolean iszidong = preferences.getBoolean("iszidong", false);
jizhumima.setChecked(isjizhu);
zidongdenglu.setChecked(iszidong);
String name=preferences.getString("name",null);
String pass=preferences.getString("pass",null);
if(isjizhu){
tel.setText(name);
pwd.setText(pass);
}
if(iszidong){
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
jizhumima.setChecked(true);
}
}
//点击事件
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.jizhumima:
if(!jizhumima.isChecked()){
zidongdenglu.setChecked(false);
}
break;
case R.id.zidongdenglu:
if(zidongdenglu.isChecked()){
jizhumima.setChecked(true);
}
break;
case R.id.tj:
strTel = tel.getText().toString();
strPwd = pwd.getText().toString();
boolean b = isCellphone(strTel);
if (strTel.isEmpty()|| strPwd.isEmpty()){
Toast.makeText(this, "手机号或密码不能为空", Toast.LENGTH_SHORT).show();
}else{
url="http://www.xieast.com/api/user/login.php?username="+ strTel +"&password="+ strPwd;
if(!b){
Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
return;
}
myPresenter.startRequest(url,1);
}
break;
case R.id.third:
break;
}
}
//正则
public static boolean isCellphone(String str) {
Pattern pattern = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$");
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
IView
@Override
public void setSuccessData(T data) {
Info info = (Info) data;
if(info.getCode()==100){
if(jizhumima.isChecked()){
edit.putBoolean("isjizhu",true);
edit.putString("name",strTel);
edit.putString("pass",strPwd);
}else{
edit.putBoolean("isjizhu",false);
edit.putBoolean("iszidong",false);
}
if(zidongdenglu.isChecked()){
edit.putBoolean("isjizhu",true);
edit.putBoolean("iszidong",true);
}else{
edit.putBoolean("iszidong",false);
}
edit.commit();
Toast.makeText(this, info.getMsg(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}else{
Toast.makeText(this, info.getMsg(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void setErrorData(T errorData) {
Toast.makeText(this, errorData+"", Toast.LENGTH_SHORT).show();
}
}