登录注册 (记住密码 sharepreference的使用)
存储数据
SharedPreferences sharedPreferences = getSharedPreferences("wujay", MODE_PRIVATE); //私有数据
Editor editor = sharedPreferences.edit();//获取编辑器
editor.putString("name", "user");
存储数据 SharedPreferences sharedPreferences = getSharedPreferences("wujay", MODE_PRIVATE); //私有数据 Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("name", "user"); editor.String("pass", password); editor.commit();//提交修改 取出数据 share = getSharedPreferences("aa", MODE_PRIVATE); String name1 = share.getString("name", "没有"); String pass1 = share.getString("pass", "没有"); 四种操作模式分别为: 1. MODE_APPEND: 追加方式存储 2. MODE_PRIVATE: 私有方式存储,其他应用无法访问 3. MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取 4. MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入 Editor editor = sharedPreferences.edit(); editor存储对象采用key-value键值对进行存放,editor.putString("name", "wujaycode"); 通过commit()方法提交数据 public class LoginActivity extends Activity { private Boolean cb; SharedPreferences share = null; String name1; String pass1; private CheckBox logincheck; @ViewInject(value = R.id.login_user) private EditText loginuser; @ViewInject(value = R.id.login_password) private EditText loginpass; @ViewInject(value = R.id.loginregist) private Button loginregist; @ViewInject(value = R.id.login) private Button login; // 登录按钮 @OnClick(R.id.login) private void myLogin(View v) { String name = loginuser.getText().toString(); String pass = loginpass.getText().toString(); //注意loginuser.equals("") && loginuser == null应该这样写比较严谨 if (loginuser.equals("") && loginuser == null) { Toast.makeText(LoginActivity.this, "请输入用户名", 1).show(); } if (loginpass.equals("") && loginpass == null) { Toast.makeText(LoginActivity.this, "请输入密码", 1).show(); } if (name.equals(name1) && pass.equals(pass1)) { Toast.makeText(LoginActivity.this, "登陆成功", 1).show(); if (logincheck.isChecked()) { Editor editor = share.edit(); editor.putString("name", name1); editor.putString("pass", pass1); editor.commit(); } Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } } // 注册按钮 @OnClick(value = R.id.loginregist) private void mylogin(View v) { Intent intent1 = new Intent(LoginActivity.this, RegistActivity.class); startActivity(intent1); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); ViewUtils.inject(LoginActivity.this); logincheck = (CheckBox) findViewById(R.id.login_check); share = getSharedPreferences("aa", MODE_PRIVATE); name1 = share.getString("name", "没有"); pass1 = share.getString("pass", "没有"); if (share.getBoolean("IsCheck", false)) { // 设置默认是记录密码状态 logincheck.setChecked(true); loginuser.setText(name1); loginpass.setText(pass1); } // 判断是否选中密码框 logincheck.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (logincheck.isChecked()) { Toast.makeText(LoginActivity.this, "记住密码选中", 1).show(); share.edit().putBoolean("IsCheck", true).commit(); } else { share.edit().putBoolean("IsCheck", false).commit(); } } }); } } 设置记住密码的步骤 1.设置点击事件setOnCheckedChangeListener判断是否选中 2.利用putboolean方法传递值 3.判断利用得到getBoolean()方法的到传递的值 4.获取编辑器,当为false时写入
editor.String("pass", password);
editor.commit();//提交修改
取出数据
share = getSharedPreferences("aa", MODE_PRIVATE);
String name1 = share.getString("name", "没有");
String pass1 = share.getString("pass", "没有");
四种操作模式分别为:
1. MODE_APPEND: 追加方式存储
2. MODE_PRIVATE: 私有方式存储,其他应用无法访问
3. MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取
4. MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入
Editor editor = sharedPreferences.edit();
editor存储对象采用key-value键值对进行存放,editor.putString("name", "wujaycode");
通过commit()方法提交数据
public class LoginActivity extends Activity {
private Boolean cb;
SharedPreferences share = null;
String name1;
String pass1;
private CheckBox logincheck;
@ViewInject(value = R.id.login_user)
private EditText loginuser;
@ViewInject(value = R.id.login_password)
private EditText loginpass;
@ViewInject(value = R.id.loginregist)
private Button loginregist;
@ViewInject(value = R.id.login)
private Button login;
// 登录按钮
@OnClick(R.id.login)
private void myLogin(View v) {
String name = loginuser.getText().toString();
String pass = loginpass.getText().toString();
//注意loginuser.equals("") && loginuser == null应该这样写比较严谨
if (loginuser.equals("") && loginuser == null) {
Toast.makeText(LoginActivity.this, "请输入用户名", 1).show();
}
if (loginpass.equals("") && loginpass == null) {
Toast.makeText(LoginActivity.this, "请输入密码", 1).show();
}
if (name.equals(name1) && pass.equals(pass1)) {
Toast.makeText(LoginActivity.this, "登陆成功", 1).show();
if (logincheck.isChecked()) {
Editor editor = share.edit();
editor.putString("name", name1);
editor.putString("pass", pass1);
editor.commit();
}
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
// 注册按钮
@OnClick(value = R.id.loginregist)
private void mylogin(View v) {
Intent intent1 = new Intent(LoginActivity.this, RegistActivity.class);
startActivity(intent1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ViewUtils.inject(LoginActivity.this);
logincheck = (CheckBox) findViewById(R.id.login_check);
share = getSharedPreferences("aa", MODE_PRIVATE);
name1 = share.getString("name", "没有");
pass1 = share.getString("pass", "没有");
if (share.getBoolean("IsCheck", false)) {
// 设置默认是记录密码状态
logincheck.setChecked(true);
loginuser.setText(name1);
loginpass.setText(pass1);
}
// 判断是否选中密码框
logincheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (logincheck.isChecked()) {
Toast.makeText(LoginActivity.this, "记住密码选中", 1).show();
share.edit().putBoolean("IsCheck", true).commit();
} else {
share.edit().putBoolean("IsCheck", false).commit();
}
}
});
}
}
设置记住密码的步骤
1.设置点击事件setOnCheckedChangeListener判断是否选中
2.利用putboolean方法传递值
3.判断利用得到getBoolean()方法的到传递的值
4.获取编辑器,当为false时写入