[Android新手学习笔记34]-Storage-SharedPreferences
来源:互联网 发布:php去掉所有html标签 编辑:程序博客网 时间:2024/06/04 01:15
使用键值对存储,值有类型区分。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button saveButton = (Button) findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 存数据
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "张3");
editor.putInt("age", 24);
editor.putBoolean("married", false);
editor.apply(); // 提交数据
// 读数据
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String name = pref.getString("name", "");
int age = pref.getInt("age", 0);
boolean married = pref.getBoolean("married", false);
Log.d("xxd", "Name:" + name + ", age:" + age + ", married:" + married);
}
});
}
}
默认路径:/data/data/<packagename>/files/,格式.xml。
记录账号密码:
public class MainActivity extends AppCompatActivity {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private CheckBox rememberCheck;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText) findViewById(R.id.account_edit);
passwordEdit = (EditText) findViewById(R.id.password_edit);
rememberCheck = (CheckBox) findViewById(R.id.remember_password);
loginButton = (Button) findViewById(R.id.login_button);
boolean isRemember = preferences.getBoolean("remember_password", false);
if (isRemember) {
// 将账号密码设置上
String account = preferences.getString("account", "");
String password = preferences.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberCheck.setChecked(true);
}
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
if (account.equals("admin") && password.equals("123456")) {
editor = preferences.edit();
if (rememberCheck.isChecked()) {
editor.putBoolean("remember_password", true);
editor.putString("account", account);
editor.putString("password", password);
} else {
editor.clear();
}
editor.apply();
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:text="账号:"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="@+id/account_edit"
android:gravity="center_vertical"
android:hint="请输入账号"
android:textSize="18sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:text="密码:"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="@+id/password_edit"
android:gravity="center_vertical"
android:inputType="textPassword"
android:hint="请输入密码"
android:textSize="18sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<CheckBox
android:id="@+id/remember_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="记住密码"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
<Button
android:id="@+id/login_button"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
0 0
- [Android新手学习笔记34]-Storage-SharedPreferences
- [Android新手学习笔记35]-Storage-SQLite
- [Android新手学习笔记36]-Storage-LitePal
- [Android新手学习笔记33]-Storage-文件存储
- Android SharedPreferences Storage
- SharedPreferences---Android学习笔记
- Android学习笔记-SharedPreferences
- Android学习笔记-SharedPreferences
- Android学习笔记12:SharedPreferences
- Android新手笔记:通过sharedpreferences实现简单的多用户注册
- Android学习笔记<20140113> Internal Storage
- Android Data Storage(数据存储)之SharedPreferences
- Android(OPhone) 学习笔记 - SharedPreferences
- android学习笔记(八)SharedPreferences、editor
- android学习笔记(八)SharedPreferences、editor
- Android学习笔记<20140112> SharedPreferences的使用
- Android 学习笔记 第五季 9 SharedPreferences
- Android学习笔记--SharedPreferences的使用
- ListView嵌套GridView出现的问题及解决方法
- 【BZOJ 1800】【AHOI 2009】飞行棋
- C#解析Cookie字符串为CookieCollection
- [Android新手学习笔记33]-Storage-文件存储
- Java中的局部变量表及使用jclasslib进行查看
- [Android新手学习笔记34]-Storage-SharedPreferences
- 【解题报告】Codeforces Round #395 (Div. 2)
- [Android新手学习笔记35]-Storage-SQLite
- Android在图片画线(放大,旋转,平移后可继续正确画线,限制画线区域)
- hdu1398(完全背包)
- 谨慎能捕千秋蝉(三)——界面操作劫持与HTML5安全
- Servlet与CGI的比较
- GitHub Top 100的Android开源库
- [Android新手学习笔记36]-Storage-LitePal