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;@Overrideprotected 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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {SaveUtils.saveName(getApplicationContext(), "test","nameBox", "t");} else {SaveUtils.saveName(getApplicationContext(), "test","nameBox", "");}}});}@Overridepublic 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;}}}
效果图:



更多相关文章

  1. Android实现手写板和涂鸦功能
  2. Android(安卓)JNI(实现自己的JNI_OnLoad函数)
  3. Android开发周刊 第四期
  4. Android(安卓)JNI(实现自己的JNI_OnLoad函数)
  5. 学习笔记_android四种点击事件方法
  6. Android(安卓)Parcelable
  7. Android无需root实现apk的静默安装
  8. Android启动画面实现
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. android中加载assets中的资源文件
  2. Android选项卡(TabWidget)应用
  3. android中加载assets中的资源文件
  4. 【翻译】(14)Stable APIs
  5. Android(安卓)Untold Stories
  6. [Android]安装使用SDK Manager更新时出现
  7. android 中调用接口发送短信
  8. AndroidStudio3.0多渠道打包
  9. 运行时改变Button的android:drawableTop
  10. android binder 面试