具体代码如下:
LoginActivity
package cn.edu.bzu.case_login;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
private CheckBox CBPass;
private EditText Name;
private EditText Password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
this.read();
}
//存
public void save(View view) {
Name = (EditText) findViewById(R.id.etName);
Password = (EditText) findViewById(R.id.etPassword);
CBPass = (CheckBox) findViewById(R.id.cbPass);
String name = Name.getText().toString();
String password = Password.getText().toString();
Boolean cbpass = CBPass.isChecked();
//正确的用户名和密码
if (name.equals("admin") && password.equals("123456")) {
SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("etName", name);
editor.putString("etPassword", password);
editor.putBoolean("Click", cbpass);
editor.commit();
Intent intent = new Intent(LoginActivity.this, WelActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
//读
public void read() {
Name = (EditText) findViewById(R.id.etName);
Password = (EditText) findViewById(R.id.etPassword);
CBPass = (CheckBox) findViewById(R.id.cbPass);
SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
String name = sp.getString("etName", "");
String password = sp.getString("etPassword", "");
boolean cbpass = sp.getBoolean("Click", false);
//判断是否选中保存密码按钮
if (cbpass == true) {
Password.setText(password);
Name.setText(name);
CBPass.setChecked(cbpass);
} else {
Password.setText("");
Name.setText(name);
CBPass.setChecked(cbpass);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74