android记住用户名密码,Android---SharedPreferences 记住用户名和密码

这篇博客详细介绍了Android中SharedPreferences的使用,包括在MainActivity中读取和保存用户设置,如用户名和密码。同时,展示了如何在另一个应用中通过测试类AccessSharePreferenceTest访问这些共享偏好设置,实现跨应用数据共享。内容涵盖了XML存储格式、权限设置以及回显和保存功能的实现。
摘要由CSDN通过智能技术生成

SharedPreferences是Android提供的一个轻量级存储类,经常用于保存软件设置参数。存放的格式为xml,文件存放在

/data/data/package name/shared_prefs下。

1.主布局文件 activity_main.xml

2.edittext_selector.xml文件

3.资源文件strings.xml

SharedPreferencesTest

用户名:

密码:

保存设置:

显示:

4.MainActivity.xml文件

public class MainActivity extends Activity {

private EditText et_name;

private EditText et_password;

private TextView resultText;

private CheckBox checkBox;

private boolean flag;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_name = (EditText) this.findViewById(R.id.name);

et_password = (EditText) this.findViewById(R.id.password);

resultText = (TextView) this.findViewById(R.id.showText);

checkBox = (CheckBox) findViewById(R.id.check);

Button button = (Button) this.findViewById(R.id.button);

Button showButton = (Button) this.findViewById(R.id.showButton);

button.setOnClickListener(listener);

showButton.setOnClickListener(listener);

// 打开软件的时候回显

SharedPreferences sharedPreferences = getSharedPreferences("ljq123",

Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

String nameValue = sharedPreferences.getString("name", "");

int ageValue = sharedPreferences.getInt("age", 1);

flag = sharedPreferences.getBoolean("checked", false);

System.out.println("flag:" + flag);

if (flag) {

checkBox.setChecked(true);

et_name.setText(nameValue);

et_password.setText(String.valueOf(ageValue));

}

}

private View.OnClickListener listener = new View.OnClickListener() {

public void onClick(View v) {

Button button = (Button) v;

// dong文件存放在/data/data//shared_prefs目录下

SharedPreferences sharedPreferences = getSharedPreferences("dong",

Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

switch (button.getId()) {

case R.id.button:

String name = et_name.getText().toString();

String password = et_password.getText().toString();

if (checkBox.isChecked()) {

flag = true;

} else {

flag = false;

}

if (flag) {

Editor editor = sharedPreferences.edit(); // 获取编辑器

editor.putString("name", name);

editor.putString("password", password);

editor.putBoolean("checked", flag);

editor.commit();// 提交修改

Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG)

.show();

}

break;

case R.id.showButton:

String nameValue = sharedPreferences.getString("name", "");

String passwordValue = sharedPreferences.getString("password",

"");

resultText.setText("姓名:" + nameValue + ",密码:" + passwordValue);

break;

}

}

};

}

运行截图:

545b4b4bfbdcc038e0919d72d82fffa6.png

访问其它应用中的SharedPreferences文件

新建一个应用AccessSharePreferenceTest,建立一个单元测试testAccessPreference:

在application节点中增加

在application节点外增加

测试类

public class AccessSharePreferenceTest extends AndroidTestCase {

private static final String TAG = "AccessSharePreferenceTest";

public void testAccessPreference() throws Exception {

Context context = this.getContext().createPackageContext(

"com.example.sharedpreferencestest",

Context.CONTEXT_IGNORE_SECURITY);

SharedPreferences sp = context.getSharedPreferences("dong",

Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

String name = sp.getString("name", "");

String password = sp.getString("password", "");

Log.d(TAG, "name:" + name + " password:" + password);

}

}

运行截图:

0cfbfc57e6d6195805a9ccc6ab04b1a3.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值