Android轻量级存储工具SharedPreferences的使用
2019-08-11 20:14
375 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43219615/article/details/99223928
1.简介
SharedPreferences是轻量级存储工具,采用的是Key-Value存储方式。一般用于存放App的个性化配置信息、临时保存的片段信息等。SharedPreferences存储键值信息是采用xml文件的形式,保存在/data/data/App包名/shared_prefs/文件名.xml(在安卓的Divice File Explorer栏可以轻易找到)。
<!--例子中的存储键-值的xml文件--> <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">小明</string> <string name="hobby">唱 跳</string> </map>
下面是使用SharedPreferences存入信息的核心代码及注释。
String name = et_name.getText().toString(); String hobby = et_hobby.getText().toString(); //第一个参数是文件名 第二个参数是操作模式,一般使用MODE_PRIVATE SharedPreferences sharedPreferences = getSharedPreferences("share", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("name", name); editor.putString("hobby", hobby); editor.commit();
下面是使用SharedPreferences读取信息的核心代码及注释。
SharedPreferences sharedPreferences = getSharedPreferences("share", Context.MODE_PRIVATE); //第二个参数表示默认值(没有这个属性的话就返回默认值) String name = sharedPreferences.getString("name",""); String hobby = sharedPreferences.getString("hobby", ""); tv_show_data.setText(String.format("读取到的数据如下:\n姓名:%s\n爱好:%s", name, hobby));
2.例子
创建SharedPreferencesActivity,代码如下。
- activity_shared_preferences.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SharedPreferencesActivity" android:orientation="vertical" android:padding="10dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="姓名:" android:textSize="18sp" android:gravity="center_vertical" android:textColor="#000000"/> <EditText android:layout_toRightOf="@id/tv_name" android:inputType="text" android:maxLength="11" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_name" android:hint="姓名"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_hobby" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="爱好:" android:textSize="18sp" android:gravity="center_vertical" android:textColor="#000000"/> <EditText android:id="@+id/et_hobby" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="爱好" android:layout_toRightOf="@id/tv_hobby"/> </RelativeLayout> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="写入共享参数"/> <Button android:layout_marginTop="50dp" android:text="读取共享参数" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_read" /> <TextView android:text="用于展示读取到的参数" android:id="@+id/tv_show_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#000000"/> </LinearLayout>
- SharedPreferencesActivity.java
package xyz.strasae.androidlearn.my; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class SharedPreferencesActivity extends AppCompatActivity { private EditText et_name; private EditText et_hobby; private TextView tv_show_data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shared_preferences); et_name = findViewById(R.id.et_name); et_hobby = findViewById(R.id.et_hobby); tv_show_data = findViewById(R.id.tv_show_data); findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = et_name.getText().toString(); String hobby = et_hobby.getText().toString(); //第一个参数是文件名 第二个参数是操作模式,一般使用MODE_PRIVATE SharedPreferences sharedPreferences = getSharedPreferences("share", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("name", name); editor.putString("hobby", hobby); editor.commit();Toast.makeText(SharedPreferencesActivity.this, "写入共享参数成功", Toast.LENGTH_SHORT).show(); } }); findViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SharedPreferences sharedPreferences = getSharedPreferences("share", Context.MODE_PRIVATE); //第二个参数表示默认值(没有这个属性的话就返回默认值) String name = sharedPreferences.getString("name",""); String hobby = sharedPreferences.getString("hobby", ""); tv_show_data.setText(String.format("读取到的数据如下:\n姓名:%s\n爱好:%s", name, hobby));Toast.makeText(SharedPreferencesActivity.this, "读取共享参数成功", Toast.LENGTH_SHORT).show(); } }); } }
相关文章推荐
- Android中使用内置的轻量级数据库SQLite存储信息
- android轻量级开源缓存框架——ASimpleCache(ACache)源码分析(封装好了一个工具类供大家使用)
- 关键字: android 工具使用 adb
- android开发环境建立以及开发工具的使用--怎样使用eclipse来开发android源码
- Android的ADB工具使用
- Android开发工具--adb的使用
- Android sqlite3工具的使用
- android基础--使用嵌入式关系型SQLite数据库存储数据
- [转]Android数据存储SharedPreferences的使用
- Android ADB工具使用
- Android开发工具--adb的使用
- Android使用SharedPreferences实现数据存储
- android数据库存储方法(一)---------------SharedPreferences(实例)记录应用程序使用次数(一)
- 【SQLiteDatabase】使用sqlite3工具在黑屏中查看android程序的内置数据库
- 使用Eclipse工具 签名【android开发】
- Android monkey工具使用(二)
- Android:当只有APK,可以使用:hierarchyview工具进行空间ID,类型查看(Robotium测试)
- Android的ADB工具使用
- Android开发中的logcat工具使用(自己还没弄明白,先收藏了)
- android hierarchyViewer 的UI工具的使用