Android 记住密码功能

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;
		}

	}

}
效果图:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值