zoukankan      html  css  js  c++  java
  • Android开发笔记(二十一)——数据存储之SharedPreference轻量级数据存储

    • xml文件,K-V形式
    • SharedPreferences:读取数据
    • SharedPreferences.Editor:写入数据

    代码示例

    在EditText中输入内容,点击保存,通过SharedPreferences来保存数据,点击显示按钮,把数据读取出来,显示在下面的TextView上。

    SharedPreferencesActivity的java代码:

    public class SharedPreferencesActivity extends AppCompatActivity {
    
        private EditText mEtName;
        private Button mBtnSave,mBtnShow;
        private TextView mTvContent;
        
        //读取数据
        private SharedPreferences mSharedPreferences;
        //写入数据
        private SharedPreferences.Editor mEditor;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_shared_preferences);
    
            mEtName=findViewById(R.id.et_name);
            mBtnSave=findViewById(R.id.btn_save);
            mBtnShow=findViewById(R.id.btn_show);
            mTvContent=findViewById(R.id.tv_content);
    
            //实例化mSharedPreferences,参数:文件名称,模式(通常使用PRIVATE表示当前文件只有本应用可以读写)
            mSharedPreferences = getSharedPreferences("data",MODE_PRIVATE); 
            //实例化mEditor
            mEditor = mSharedPreferences.edit();
    
            mBtnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //实现把EditText的文本通过SharedPreferences保存起来
                    //putString写入数据:键值对,参数:key,value
                    mEditor.putString("name",mEtName.getText().toString()); //mEtName.getText().toString()获取EditText文本的内容
                    mEditor.apply();
                }
            });
    
            mBtnShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //getString读出数据,参数:key,default(缺省的值,设为空即可)
                    mTvContent.setText(mSharedPreferences.getString("name",""));
                }
            });
        }
    }
    

    对应的布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="15dp">
    
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入内容"
            />
    
        <Button
            android:id="@+id/btn_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="保存"
            android:layout_marginTop="10dp"
            />
    
        <Button
            android:id="@+id/btn_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示"
            android:layout_marginTop="10dp"
            />
    
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            />
    
    </LinearLayout>
    

    运行效果

    文件保存位置:

    在Android系统中,其配置文件的数据文件 以XML文件的形式保存在:/data/data/<applicationId>/shard_prefs

    <applicationId> 默认是包名,但其本质不是包名

    这个data.xml文件就是通过SharedPreference存储之后生成的一个xml文件

    总结

    //定义读写对象
    SharedPreferences sharedPreferences = getSharedPreferences(“参数名”,MODE_PRIVATE)
    SharedPreferences.Editor editor = sharedPreferences.edit()
    //写入数据
    editor.putString(“参数名”,editText.getText().toString())
    editor.apply();
    //读出数据
    sharedPreferences.getString(“参数名”,"")
    
  • 相关阅读:
    Android接入WebView
    james邮件服务器部署
    防止网络攻击的方式
    vue开发遇到的问题及解决方式
    jekins和docker的作用
    设计模式(2)[JS版]---JavaScript如何实现单例模式?
    黑客帝国中代码雨如何实现?用 canvas 轻松实现代码雨炫酷效果!
    什么是JavaScript 的闭包???
    纯CSS实现iOS风格打开关闭选择框
    纯CSS实现自定义单选框和复选框
  • 原文地址:https://www.cnblogs.com/yangdd/p/13373015.html
Copyright © 2011-2022 走看看