布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">
<TextView
android:layout_marginLeft="30dp"
android:layout_width="30dp"
android:layout_height="50dp"
android:text="用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/name"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="密码"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/password"/>
</LinearLayout>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/jizhu"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/denglu"
android:text="登录"/>
</LinearLayout>
view层
public interface LoginView {
void success(String result);
void failer(String result);
void show();
void hide();
}
CallBack层
public interface CallBack {
void onSussecc(String surl);
void onFailre(String surl);
}
presenter层
public class LoginPresenter {
private LoginView mloginView;
private final LoginBiz loginBiz;
public LoginPresenter(LoginView mloginView) {
this.mloginView = mloginView;
loginBiz = new LoginBiz();
}
public void login(String name,String password){
mloginView.show();
loginBiz.login(name, password, new CallBack() {
@Override
public void onSussecc(String surl) {
mloginView.success(surl);
mloginView.show();
}
@Override
public void onFailre(String surl) {
mloginView.failer(surl);
mloginView.hide();
}
});
}
}
LoginBiz层
public class LoginBiz {
public void login(String name, String password, CallBack callBack){
if (name.equals("123")&&password.equals("123")){
callBack.onSussecc("登录成功");
}else{
callBack.onFailre("登录失败");
}
}
}
主页面
public class MainActivity extends AppCompatActivity implements LoginView{
private CheckBox jizhu;
private Button denglu;
private EditText password;
private EditText name;
private LoginPresenter loginPresenter;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginPresenter = new LoginPresenter(this);
//获取控件
initView();
//获取数据
initDate();
}
private void initDate() {
sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
denglu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, name.getText().toString().trim() + password.getText().toString().trim(), Toast.LENGTH_SHORT).show();
loginPresenter.login(name.getText().toString().trim(), password.getText().toString().trim());
}
});
//判断
if (sharedPreferences.getBoolean("记住密码", false)) {
String sname = sharedPreferences.getString("用户名", "");
String spassword = sharedPreferences.getString("密码", "");
boolean b = sharedPreferences.getBoolean("记住密码", false);
jizhu.setChecked(b);
name.setText(sname);
password.setText(spassword);
}
}
private void initView() {
name = findViewById(R.id.name);
password = findViewById(R.id.password);
denglu = findViewById(R.id.denglu);
jizhu = findViewById(R.id.jizhu);
}
@Override
public void success(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
//存值
if (jizhu.isChecked()) {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("用户名", name.getText().toString().trim());
edit.putString("密码", password.getText().toString().trim());
edit.putBoolean("记住密码", true);
edit.commit();
}
Intent intent = new Intent(MainActivity.this, TowActivity.class);
startActivity(intent);
finish();
}
@Override
public void failer(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
@Override
public void show() {
}
@Override
public void hide() {
}
}