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();

存储的类型

最新文章

  1. python 小功能
  2. redis 一二事 - 搭建集群缓存服务器
  3. matlab如何连同换行也输入txt中
  4. 神奇的HTML5离线存储(应用程序缓存)
  5. ZOJ1238 Guess the Number
  6. SLF4J日志门面
  7. Dropbox 有哪些鲜为人知的使用技巧?
  8. Juqery 中使用 ajax
  9. Error getting nested result map values for &#39;company&#39;. Cause: java.sql.SQLException: Invalid value for getInt() - &#39;NFHK188&#39;
  10. 从PRISM开始学WPF(二)Prism?
  11. 记录一次DataTable排序的问题
  12. mysql 表关联批量更新
  13. android SeekBar设置背景无法被填充满的bug
  14. 【Android开发经验】android:windowSoftInputMode属性具体解释
  15. 2018-2019-1 20189203《linux内核原理与分析》第六周作业
  16. ubuntu 16.04 忘记root密码
  17. Shell编程学习1--基础了解
  18. nyoj 37 回文字符串 【DP】
  19. WPF界面框架的设计
  20. 2820: YY的GCD

热门文章

  1. js中的数据类型,以及如何检测数据类型
  2. BigDecimal 使用浅析
  3. 边缘缓存模式(Cache-Aside Pattern)
  4. java设计模式8.迭代子模式、责任链模式、命令模式
  5. 手机端apk文件安装
  6. hdu 6092 Rikka with Subset(多重背包)
  7. CodeForces Round #514 (div2)
  8. Codeforces Round #383 (Div. 2) C. Arpa&#39;s loud Owf and Mehrdad&#39;s evil plan(dfs+数学思想)
  9. CM &amp; CDH 基本概念
  10. 02 python 必知