Android笔记(三十八) Android中的数据存储——SharedPreferences
2024-10-21 16:07:56
SharedPreferences是Android提供的一种轻型的数据存储方法,其本质是基于xml文件存储的,内部数据以key-value的方式存储,通常用来存储一些简单的配置信息。
SharedPreferences对象本身只能获取数据而不支持修改和存储,存储修改需要通过Editor对象来实现。
使用SharedPreperences保存数据
使用SharedPreperences来保存数据的步骤如下:
1. 实例化SharedPreperences对象
2. 实例化Editor对象
3. editor.put方法保存数据
4. editor.commit提交数据
例子:
MainActivity.java
package cn.lixyz.sharedpreferencesdemo; import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText username;
private EditText password;
private Button login;
private Button register; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.register); login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp = getSharedPreferences("username", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", username.getText().toString());
editor.putString("password", password.getText().toString());
if (editor.commit()) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入您的用户名"
android:textSize="30dp" /> <EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入您的密码"
android:password="true"
android:textSize="30dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录" /> <Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册" />
</LinearLayout> </LinearLayout>
运行结果:
我们去DDMS中查找有没有一个username.xml文件,果然有:
导出打开:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="username">WWWADS</string>
<string name="password">WASDFGHA</string>
</map>
可以看到我们刚才输入的用户名和密码保存成功了。
我们使用put存储数据的时候我们会看到
由此可见,SharedPreferences只能够存储一些基本数据类型的数据。
使用SharedPreperences来读取数据
我们来模拟一个记住用户名的操作:
MainActivity.java
package cn.lixyz.sharedpreferencesdemo; import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText username;
private EditText password;
private Button login;
private Button register;
private CheckBox remberUser;
private TextView forgetPassword;
SharedPreferences sp ;
SharedPreferences.Editor editor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.register);
remberUser = (CheckBox) findViewById(R.id.remberUser);
forgetPassword = (TextView) findViewById(R.id.forgetPassword); sp = getSharedPreferences("username", MODE_PRIVATE);
editor = sp.edit(); String spUsername = sp.getString("username","");
if(spUsername == null){
remberUser.setChecked(false);
}else{
username.setText(spUsername);
remberUser.setChecked(true);
} login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(remberUser.isChecked()){
editor.putString("username", username.getText().toString());
if (editor.commit()) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入您的用户名"
android:textSize="30dp" /> <EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入您的密码"
android:password="true"
android:textSize="30dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录" /> <Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<CheckBox
android:id="@+id/remberUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名"/> <TextView
android:id="@+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:text="忘记密码"/>
</LinearLayout> </LinearLayout>
最新文章
- Android Studio no debuggable applications解决方案
- nios II--实验1——hello_world软件部分
- MYSQL 【汇总数据】 【分组数据】 学习记录
- LevelDB:一个快速轻量级的key-value存储库(译)
- 关于执行ST_Geometry的st_centroid函数时报ORA-28579错误的问题
- Activiti 工作流得到最后一次批注的时间
- 针对苹果最新审核要求:应用兼容IPv6
- spark-submit
- [放松一下] 经典高清电影合集 170G BT种子下载
- 于ubuntu配置hadoop当问题
- 201521123026《Java程序设》 第10周学习总结
- centos7 安装freetype
- 【建模应用】PCA主成分分析原理详解
- Mysql授权root用户远程登录
- spring事物深入了解
- EF 通过修改模版 更改生成实体名称
- HDU6198
- python __all__用法
- 核心交换机各项配置 Vlan划分、互访、ACL管控、链路聚合等
- VSCode打开已有vuejs项目
热门文章
- 关于idea跳过错误编译的理解, 跳过报错的代码启动项目去debug测试其他正常的代码
- python2中的unicode()函数在python3中会报错:
- flink入门(一)——基本原理与应用场景
- Sql server 中将数据行转列列转行(二)
- 如何在LabWIndows/CVI中调用LabVIEW DLL
- node + promise 实现文件读写
- JAVA代码:生成一个集合,自定义大小,100以内的随机整数
- POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
- 深度解析qml引擎---(1)Qml文件加载
- 【剑指offer】面试题 22. 链表中倒数第 K 个节点