安卓开发之使用SharedPreferences保存键值对数据

有时候,不是因为你没有能力,也不是因为你缺少勇气,只是因为你付出的努力还太少,所以,成功便不会走向你。而你所需要做的,就是坚定你的梦想,你的目标,你的未来,然后以不达目的誓不罢休的那股劲,去付出你的努力,成功就会慢慢向你靠近。

导读:本篇文章讲解 安卓开发之使用SharedPreferences保存键值对数据,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

使用SharedPreferences保存键值对数据

如果您有想要保存的相对较小键值对集合,则应使用 SharedPreferences API。SharedPreferences 对象指向包含键值对的文件,并提供读写这些键值对的简单方法。每个 SharedPreferences 文件均由框架进行管理,可以是私有文件,也可以是共享文件。

注意:SharedPreferences API仅仅提供了读写键值对的功能,你不要和Preference
API相混淆,他们不是一码事。后者可以帮助你创建一个设置用户配置的UI(尽管也会使用SharedPreferences来保存app的设置)

AndroidManifest.xml

 <activity android:name=".SaveData">
        <intent-filter>
            <action android:name="sp" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

savedata.xml

在这里插入图片描述

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_input"
                android:layout_width="287dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="30dp"
                android:layout_weight="1"
                android:textSize="20dp"></EditText>

            <Button
                android:id="@+id/btn_saveContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:onClick="save"
                android:text="存入SHAREDPREFENCE"
                android:textColor="#090909"
                android:textSize="20dp"
                app:backgroundTint="#E3E2E2"></Button>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="30dp">

            <TextView
                android:id="@+id/tv_result"
                android:layout_width="332dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:textSize="20dp"></TextView>

            <Button
                android:id="@+id/btn_showContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:textColor="#090909"
                android:text="从SHAREDPREFERENCE取出"
                app:backgroundTint="#E3E2E2"
                android:textSize="20dp"></Button>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

savedata

  1. 获取共享偏好设置(Shared Preferences)的句柄(handle) 写入
  2. Shared Preferences
  3. 从共享Preference文件中读取数据
package com.example.myapplication4;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SaveData extends AppCompatActivity implements View.OnClickListener {
    private EditText et_content;
    private TextView tv_content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_savedata);
        findViewById(R.id.btn_saveContent).setOnClickListener(this);
        findViewById(R.id.btn_showContent).setOnClickListener(this);
        tv_content = (TextView) findViewById(R.id.tv_result);
        et_content = (EditText) findViewById(R.id.et_input);
        if(tv_content.getText().toString().trim().equals("")){
            SharedPreferences sp = this.getSharedPreferences("data", MODE_PRIVATE);
            tv_content.setText(sp.getString("content", ""));
            et_content.setText(sp.getString("content", ""));
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_saveContent:
                SharedPreferences sp = this.getSharedPreferences("data", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("content", et_content.getText().toString().trim());
                editor.commit();
                Toast.makeText(this, "存入成功", Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_showContent:
                sp = this.getSharedPreferences("data", MODE_PRIVATE);
                tv_content.setText(sp.getString("content", ""));
                Toast.makeText(this, "取出成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }




}

实验结果

在这里插入图片描述

总结

使用两种方法来获取SharedPreferences对象:

getSharedPreferences() – 该方法的第一个参数为preferences文件名,该方法用于区分不同的若干preferences文件;
getPreferences() – 如果你的activity只使用一个preferences文件,该方法不需要文件名。

写入值:

调用edit()来获取一个SharedPreferences.Editor对象,使用该editor对象的putXXX()方法来写入值,最后通过commit()方法,整体提交数据的修改。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/147393.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!