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>
相关推荐
不修×蝙蝠2 小时前
eclipse使用 笔记02
前端·笔记·后端·eclipse
未 顾2 小时前
JavaWeb纯小白笔记02:Tomcat的使用:发布项目的三种方式、配置虚拟主机、配置用户名和密码
java·笔记·tomcat
Deryck_德瑞克2 小时前
mysql笔记—sql性能分析
笔记·sql·mysql
木木夕阳子2 小时前
Go语言笔记
笔记
K1t02 小时前
Android-UI设计
android·ui
吃汉堡吃到饱4 小时前
【Android】浅析MVC与MVP
android·mvc
开MINI的工科男8 小时前
【笔记】自动驾驶预测与决策规划_Part3_路径与轨迹规划
人工智能·笔记·自动驾驶·预测与决策
深海呐10 小时前
Android AlertDialog圆角背景不生效的问题
android
ljl_jiaLiang10 小时前
android10 系统定制:增加应用使用数据埋点,应用使用时长统计
android·系统定制
花花鱼10 小时前
android 删除系统原有的debug.keystore,系统运行的时候,重新生成新的debug.keystore,来完成App的运行。
android