Android [SharedPreference轻量级存储]
2024-09-01 00:23:22
SharedPreferencesActivity.java
package com.xdw.a122.data; import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import com.xdw.a122.R; public class SharedPreferencesActivity extends AppCompatActivity {
private EditText mEtName;
private Button mBtnSave,mBtnShow;
private TextView mTvContent;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
mEtName=findViewById(R.id.et_name);
mBtnSave=findViewById(R.id.btn_save);
mBtnShow=findViewById(R.id.btn_show);
mTvContent=findViewById(R.id.tv_show); mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE); //名称和类型
mEditor=mSharedPreferences.edit(); mBtnSave.setOnClickListener(new View.OnClickListener() { //存储
@Override
public void onClick(View v) {
mEditor.putString("name",mEtName.getText().toString());
//此处写要提交的内容
mEditor.apply();
//or commit(); 存储完成
}
});
mBtnShow.setOnClickListener(new View.OnClickListener() { //读取
@Override
public void onClick(View v) {
mTvContent.setText(mSharedPreferences.getString("name",""));
}
});
}
}
activity_shared_preferences
<?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=".data.SharedPreferencesActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入内容"
/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>
输入内容在 //存储 输入存储内容
并在tv_show显示
mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE); //名称和类型
mEditor=mSharedPreferences.edit();
存储的类型
最新文章
- python 小功能
- redis 一二事 - 搭建集群缓存服务器
- matlab如何连同换行也输入txt中
- 神奇的HTML5离线存储(应用程序缓存)
- ZOJ1238 Guess the Number
- SLF4J日志门面
- Dropbox 有哪些鲜为人知的使用技巧?
- Juqery 中使用 ajax
- Error getting nested result map values for &#39;company&#39;. Cause: java.sql.SQLException: Invalid value for getInt() - &#39;NFHK188&#39;
- 从PRISM开始学WPF(二)Prism?
- 记录一次DataTable排序的问题
- mysql 表关联批量更新
- android SeekBar设置背景无法被填充满的bug
- 【Android开发经验】android:windowSoftInputMode属性具体解释
- 2018-2019-1 20189203《linux内核原理与分析》第六周作业
- ubuntu 16.04 忘记root密码
- Shell编程学习1--基础了解
- nyoj 37 回文字符串 【DP】
- WPF界面框架的设计
- 2820: YY的GCD
热门文章
- js中的数据类型,以及如何检测数据类型
- BigDecimal 使用浅析
- 边缘缓存模式(Cache-Aside Pattern)
- java设计模式8.迭代子模式、责任链模式、命令模式
- 手机端apk文件安装
- hdu 6092 Rikka with Subset(多重背包)
- CodeForces Round #514 (div2)
- Codeforces Round #383 (Div. 2) C. Arpa&#39;s loud Owf and Mehrdad&#39;s evil plan(dfs+数学思想)
- CM &; CDH 基本概念
- 02 python 必知