Android : SharedPreferences存储xml_简单应用

MainActivity.java

java 复制代码
package com.example.mysharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btnCommit,btnGetdata;
    private EditText editText;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCommit = findViewById(R.id.btn_submit);
        btnGetdata = findViewById(R.id.btn_get_data);
        editText = findViewById(R.id.et_content);
        textView = findViewById(R.id.tv_see);

        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /** 开始保存数据
                 * xml文件名
                 * 文件访问模式
                 * MODE_PRIVATE         私有模式,只能本应用程序操作,会替换内容
                 *
                 * MODE_WORLD_READABLE          当前文件可以被其它应用读取
                 *
                 * MODE_WORLD_WRITEABLE          当前文件可以被其它应用写入
                 * */
                SharedPreferences sp=getSharedPreferences("data_xml", MODE_PRIVATE);
                /** 1.获取一个编辑对象 */
                SharedPreferences.Editor editor =sp.edit();
                /** 2.写数据保存 */
                String data = editText.getText().toString();
                editor.putString("textContent",data);
                /** 3.提交编辑 */
                editor.commit();

                Toast.makeText(getApplication(),"保存成功", Toast.LENGTH_SHORT).show();

            }
        });

        //读取xml文件
        btnGetdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /** 判断之前是否保存了数据 */
                /** 获取SharedPreferences如果之前已经存在就直接获取 反之 创建一个新的文件对象  */
                SharedPreferences sp = getSharedPreferences("data_xml",MODE_PRIVATE);
                /**
                 * sp.getString("textSizeData","默认值")
                 * 去文件对象中的值
                 * 如果值不存在 返回默认值 "默认值"
                 * */
                textView.setText(sp.getString("textContent","默认值"));

            }
        });
    }
}

activity_main.xml

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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="sharedpreferences保存xml数据"
        />

    <EditText
        android:textSize="24sp"
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

    <Button
        android:textSize="24sp"
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击保存"
        />

    <Button
        android:textSize="24sp"
        android:id="@+id/btn_get_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击读取"
        />

    <TextView
        android:id="@+id/tv_see"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="数据"
         />

</LinearLayout>
相关推荐
巴伦是只猫32 分钟前
【机器学习笔记Ⅰ】11 多项式回归
笔记·机器学习·回归
DKPT4 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
巴伦是只猫6 小时前
【机器学习笔记Ⅰ】13 正则化代价函数
人工智能·笔记·机器学习
哲科软件6 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin
X_StarX11 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
MingYue_SSS12 小时前
开关电源抄板学习
经验分享·笔记·嵌入式硬件·学习
巴伦是只猫12 小时前
【机器学习笔记 Ⅱ】1 神经网络
笔记·神经网络·机器学习
weixin_4373982112 小时前
转Go学习笔记(2)进阶
服务器·笔记·后端·学习·架构·golang
jyan_敬言12 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
程序员老刘13 小时前
Android 16开发者全解读
android·flutter·客户端