android 当中有集中数据存储方式,比如说sqlite,还有一个比较轻量级的,那就是SharedPreferences,记住密码功能也可以用这两种方法存储,我用的是SharedPreferences,这里也就介绍SharedPreferences。
如果第一次使用SharedPreferences,他会在/data/data/包命/shared_prefs/下生成xxx.xml,这个xxx.xml就是存储你的键值对,要实现记住密码功能,代码(工具类,用来将密码存入xml中和读取xml的密码):
package com.todoo.android.app.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
*
* @author WangJintao
*
* 2013-4-17
*/
public class SaveUserInfUtils {
/**
* 保存用户名
*
* @param context
* @param name
* @param key
* @param value
* @return
*/
public static boolean saveName(Context context, String name, String key,
String value) {
SharedPreferences preferences = context.getSharedPreferences(name,
context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
/**
* 获取用户名
*
* @param context
* @param name
* @param key
* @return
*/
public static String getName(Context context, String name, String key) {
SharedPreferences preferences = context.getSharedPreferences(name,
context.MODE_PRIVATE);
return preferences.getString(key, null);
}
}
实现记住密码:
package com.example.savapsw;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
EditText nameText, pswText;
CheckBox nameBox, pswBox;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText) findViewById(R.id.user_name);
pswText = (EditText) findViewById(R.id.user_psw);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
nameBox = (CheckBox) findViewById(R.id.save_name);
pswBox = (CheckBox) findViewById(R.id.save_psw);
// nameBox.setOnClickListener(this);
// pswBox.setOnClickListener(this);
// if (nameBox.isChecked()) {
if ("t".equals(SaveUtils.getName(getApplicationContext(), "test",
"nameBox"))) {
nameBox.setChecked(true);
}
nameText.setText(SaveUtils.getName(getApplicationContext(), "test",
"name"));
// }
// if (pswBox.isChecked()) {
pswText.setText(SaveUtils.getName(getApplicationContext(), "test",
"psw"));
// }
nameBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
SaveUtils.saveName(getApplicationContext(), "test",
"nameBox", "t");
} else {
SaveUtils.saveName(getApplicationContext(), "test",
"nameBox", "");
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
if (nameBox.isChecked()) {
nameBox.setChecked(true);
String name = nameText.getText().toString();
SaveUtils.saveName(getApplicationContext(), "test", "name",
name);
} else {
SaveUtils.saveName(getApplicationContext(), "test", "name", "");
}
this.finish();
break;
default:
break;
}
}
}
效果图: