Androidパスワード記憶機能
3680 ワード
Androidには集中データ格納方式があります.例えばsqlite、もう一つの軽量級のものがあります.それはSharedPreferencesです.パスワードを覚える機能もこの2つの方法で格納できます.私はSharedPreferencesを使っています.ここではSharedPreferencesについて紹介します.
SharedPreferencesを初めて使うと、/data/data/包命/shared_prefs/でxxxを生成する.xml、このxxx.xmlはあなたのキー値のペアを保存して、パスワードを覚える機能を実現するには、コード(ツールクラス、パスワードをxmlに保存したり、xmlを読み取ったりするためのパスワード):
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;
}
}
}
効果図: