06-数据存储
2021/9/9 23:05:49
本文主要是介绍06-数据存储,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
SharedPreference数据的读写操作:
MainActivity.java:
package com.imooc.sharedpreference; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; 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 { //1.7====================== static final String KEY = "MyValue"; //========================= //1.2====================== private EditText et; //========================= //1.5====================== SharedPreferences preferences; SharedPreferences.Editor editor; //================================= @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.3================================================ et = (EditText) findViewById(R.id.editTextTextPersonName); //=================================================== //1.5============================================= preferences = getPreferences(Activity.MODE_PRIVATE); editor = preferences.edit(); //================================================ //1.4======================================================================== findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String in = preferences.getString(KEY,"当前数值不存在"); Toast.makeText(getApplicationContext(),in,Toast.LENGTH_SHORT).show(); } }); findViewById(R.id.writeBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1.6==================== editor.putString(KEY,et.getText().toString()); if(editor.commit()){ Toast.makeText(getApplicationContext(),"写入成功",Toast.LENGTH_SHORT).show(); } //======================= } }); //===================================================================================== } }
activity_main.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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> //1.1======================================================= <EditText android:id="@+id/editTextTextPersonName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" /> <Button android:id="@+id/readBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="读取数据" tools:layout_editor_absoluteX="14dp" tools:layout_editor_absoluteY="131dp" /> <Button android:id="@+id/writeBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="写入数据" tools:layout_editor_absoluteX="9dp" tools:layout_editor_absoluteY="179dp" /> //============================================================= </LinearLayout>
PreferenceActivity使用方法:
这篇关于06-数据存储的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?