**
利用SharedPreferences实现用户记住密码和自动登录
**
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据。
获取SharedPreferences的两种方式:
1 调用Context对象的getSharedPreferences()方法
2 调用Activity对象的getPreferences()方法
两种方式的区别:
调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.
调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.
本实验使用SharedPreferences实现用户记住密码和自动登录:
效果图:
布局文件:
<TextView
android:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/tvName"
android:textAppearance="?android:attr/textAppearanceMedium" /><EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvUsername"
android:layout_below="@+id/tvUsername"
android:background="@android:drawable/edit_text"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etUsername"
android:layout_below="@+id/etUsername"
android:text="@string/tvPassword"
android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPassword"
android:layout_below="@+id/tvPassword"
android:layout_marginTop="16dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/rem_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etPassword"
android:layout_below="@+id/etPassword"
android:text="@string/rem_pwd" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/etPassword"
android:layout_below="@+id/zd_login"
android:background="#FF72CAE1"
android:text="@string/btnLogin"
/>
<CheckBox
android:id="@+id/zd_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/rem_pwd"
android:layout_alignBottom="@+id/rem_pwd"
android:layout_centerHorizontal="true"
android:text="@string/zd_login" />`
后台实现
“`
package org.bzu.logindemo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class LoginMainActivity extends Activity {
CheckBox rem_pwd,zd_login;
Button button;
EditText tname, tpass;
SharedPreferences preferences;
Editor editor;
String uname, upass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_main);
preferences = getSharedPreferences("data", MODE_PRIVATE);
findId();
login_read();
//记住密码checkedbox状态监听
rem_pwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (rem_pwd.isChecked()) {
preferences.edit().putBoolean("rem_pwd", true).commit();
} else {
preferences.edit().putBoolean("rem_pwd", false).commit();
}
}
});
//自动登陆checkedbox状态监听
zd_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (zd_login.isChecked()) {
preferences.edit().putBoolean("zd_login", true).commit();
} else {
preferences.edit().putBoolean("zd_login", false).commit();
}
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
uname = tname.getText().toString();
upass = tpass.getText().toString();
if (uname.equals("123") && upass.equals("123")) {
Toast.makeText(LoginMainActivity.this, "登录成功",
Toast.LENGTH_SHORT).show();
// 登录成功和记住密码框为选中状态才保存用户信息
if (rem_pwd.isChecked()) {
// 记住用户名、密码、
Editor editor = preferences.edit();
editor.putString("USER_NAME", uname);
editor.putString("PASSWORD", upass);
editor.commit();
}
// 跳转界面
Intent intent = new Intent(LoginMainActivity.this,LoginSuccessActivity.class);
intent.putExtra("name", uname);
startActivity(intent);
} else {
Toast.makeText(LoginMainActivity.this, "用户名或密码错误,请重新登录",
Toast.LENGTH_SHORT).show();
}
}
});
}
public void findId() {
rem_pwd = (CheckBox) findViewById(R.id.rem_pwd);
zd_login=(CheckBox) findViewById(R.id.zd_login);
button = (Button) findViewById(R.id.btnLogin);
tname = (EditText) findViewById(R.id.etUsername);
tpass = (EditText) findViewById(R.id.etPassword);
}
public void login_read() {
if (preferences.getBoolean("rem_pwd", false)) {// 判断记住密码框选中状态。不存在返回false
rem_pwd.setChecked(true);// 设置checkedbox的状态为true
tname.setText(preferences.getString("USER_NAME", ""));
tpass.setText(preferences.getString("PASSWORD", ""));
if(preferences.getBoolean("zd_login", false)){
//设置默认是自动登录状态
zd_login.setChecked(true);
//跳转界面
Intent intent = new Intent(LoginMainActivity.this,LoginSuccessActivity.class);
intent.putExtra("name", tname.getText().toString());
startActivity(intent);
}
}
}
}
`