布局文件:
<?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:gravity="center"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 密码 " />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</LinearLayout>
<CheckBox
android:id="@+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
LoginView
public interface LoginView {
void onSuccess(String result);
void onFailed(String msg);
void show();
void hide();
}
LoginBiz
public class LoginBiz {
public void login(final String name, final String password, final LoginCallBack callBack){
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... strings) {
if (name.equals("Baway")&&password.equals("123")){
return "成功";
}else {
return "用户名或密码错误";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s.contains("成功")){
callBack.onLoginSuccess("登录成功");
}else {
callBack.onLoginFailed("用户名或密码错误");
}
}
}.execute();
}
}
LoginPresenter
public class LoginPresenter {
private LoginView loginView;
private LoginBiz loginBiz;
public LoginPresenter(LoginView loginView) {
this.loginView = loginView;
loginBiz = new LoginBiz();
}
public void login(String name,String password){
loginView.show();
loginBiz.login(name, password, new LoginCallBack() {
@Override
public void onLoginSuccess(String result) {
loginView.hide();
loginView.onSuccess(result);
}
@Override
public void onLoginFailed(String msg) {
loginView.hide();
loginView.onFailed(msg);
}
});
}
}
MainActivity实现LoginView
public class MainActivity extends AppCompatActivity implements LoginView {
private EditText name;
private EditText password;
private Button login;
private ProgressBar progressBar;
private CheckBox box;
private LoginPresenter loginPresenter;
private SharedPreferences sp;
private String username;
private String pwd;
private Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
box = (CheckBox) findViewById(R.id.box);
//获取Preferences
sp = getSharedPreferences("info",Context.MODE_PRIVATE);
if (sp.getBoolean("记住密码",false)){
String user = sp.getString("name", "");
String pass = sp.getString("password", "");
box.setChecked(true);
name.setText(user);
password.setText(pass);
}
loginPresenter = new LoginPresenter(this);
//设置点击监听事件
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(MainActivity.this.name.getText().toString().trim()) || TextUtils.isEmpty(password.getText().toString().trim())) {
Toast.makeText(MainActivity.this, "用户名或密码为空", Toast.LENGTH_SHORT).show();
} else {
username = name.getText().toString().trim();
pwd = password.getText().toString().trim();
loginPresenter.login(username, pwd);
}
}
});
}
@Override
public void onSuccess(String result) {
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
if (box.isChecked()){
edit = sp.edit();
edit.putString("name",username);
edit.putString("password",pwd);
edit.putBoolean("记住密码",true);
}
edit.commit();
startActivity(new Intent(this, TwoActivity.class));
}
@Override
public void onFailed(String msg) {
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
@Override
public void show() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void hide() {
progressBar.setVisibility(View.GONE);
}
}