Android使用SharedPreferneces存储数据

news/2024/7/7 9:18:44

Android使用SharedPreferneces存储数据
实现效果:
在这里插入图片描述
java代码:

package com.henu.sharedpreferences;

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 MainActivity extends AppCompatActivity {

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

    public void save(View v) {
        EditText etcontent = (EditText) findViewById(R.id.et_content);
        SharedPreferences sp = this.getSharedPreferences("data", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("content", etcontent.getText().toString().trim());
        editor.commit();
        Toast.makeText(this, "存入成功", Toast.LENGTH_SHORT).show();
    }

    public void display(View v) {
        TextView tvcontent = (TextView) findViewById(R.id.tv_content);
        SharedPreferences sp = this.getSharedPreferences("data", MODE_PRIVATE);
        tvcontent.setText(sp.getString("content", ""));
        Toast.makeText(this, "取出成功", Toast.LENGTH_SHORT).show();
    }
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <EditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:textSize="35dp"
            android:layout_marginTop="40dp"></EditText>

        <Button
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="存入SHAREDPREFERENCES"
            android:layout_gravity="center"
            android:onClick="save"
            android:textSize="35dp"></Button>
        <Button
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="从SHAREDPREFERENCES取出"
            android:layout_gravity="center"
            android:onClick="display"
            android:textSize="33dp"></Button>
        <TextView
            android:textSize="35dp"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/tv_content"></TextView>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

**注意:**在写Button的android:onClick="save"点击事件不要忘了在定义函数时 public void save(View v) 加入形参 View v


http://www.niftyadmin.cn/n/3649782.html

相关文章

typescript 模块_TypeScript中的模块增强

typescript 模块Before getting into module augmentation, let’s look at some TypeScript merging principles which will become useful as we progress. 在进行模块扩充之前&#xff0c;让我们看一下一些TypeScript合并原理&#xff0c;这些原理将随着我们的进步而变得有用…

阴阳怕懵懂吗?

阴阳怕懵懂吗&#xff1f;郑昀 20060621 最近看来势必要有一批人跟着麦田的《 阴阳怕懵懂&#xff0c;说说“无名小站” - 麦田的读书生活 》和方兴东的《 创业者最核心的品质是什么&#xff1f;对模糊性的高度容忍--方兴东观察》的博文继续往下谈。嗯&#xff0c;这就是话题的…

Android开发使用SQLite数据库和Listview实现数据的存储与展示

Android开发使用SQLite数据库和Listview实现数据的存储与展示 实现效果&#xff1a; 使用了SimpleCursorAdapter方便数据库和listview之间的数据传递。 MainActivity.java&#xff1a; package com.henu.saveindatabase;import androidx.appcompat.app.AppCompatActivity;i…

js更改css属性_CSS会更改属性:何时以及何时不使用它

js更改css属性The will-change CSS property is commonly misunderstood or not used correctly, in this short post I will demystify the property so you can make your CSS animations and transitions as performant as possible. will-change CSS属性通常被误解或使用不…

在不断地试错中调整向前

在不断地试错中调整向前郑昀 20060619 几个月前的这两篇文章《Web2.0创业&#xff1a;程序员的创业&#xff1f; - 玩火者的自白 - 邱致中的IT博客》和《Web2.0创业&#xff1a;需要什么样的团队》说的模模糊糊&#xff0c;我们看得也是懵懵懂懂。本来许多事情就是要在不断地…

contentProviderSample实现操作数据库

contentProviderSample实现操作数据库并显示在listview控件中。 实现效果和上个实验相同&#xff1a; 点击进入上个实验 本实验在上个实验基础上完成&#xff0c;与上个实验包名略有不同&#xff0c;并且添加了ContentProvider.java文件、修改了MainActivity.java的代码。 Con…

node.js入门_Node.js中的压缩入门

node.js入门Compression in Node.js and Express decreases the downloadable amount of data that’s served to users. Through the use of this compression, we can improve the performance of our Node.js applications as our payload size is reduced drastically. Nod…

跨域或者Internet访问Remoting[Remoting FAQ]

[Remoting FAQ]跨域或者Internet访问RemotingVersionDateCreatorDescription1.0.0.12006-6-1郑昀Ultrapower草稿继续阅读之前&#xff0c;我们假设您熟悉以下知识&#xff1a;n Remoting[需求]虽然说&#xff0c;Remoting一般都在同一个域内调用&#xff0c;但有时候&a…