1. 最后生成xml文件 k-v形式
  2. SharedPreferences
  3. SharedPreferences.Editor

主线
DataStorageActivity以及对应的xml

package com.example.test0508.dataStorage;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import com.example.test0508.R;public class DataStorageActivity extends AppCompatActivity implements View.OnClickListener {    private Button mBtnSharedPreferences;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_data_storage);        mBtnSharedPreferences = findViewById(R.id.btn_shared_preferences);        mBtnSharedPreferences.setOnClickListener(this);    }    @Override    public void onClick(View view) {        Intent intent = null;        switch (view.getId()){            case R.id.btn_shared_preferences:                intent = new Intent(DataStorageActivity.this,SharedPreferencesActivity.class);                break;        }        startActivity(intent);    }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="10dp"    >    <Button        android:id="@+id/btn_shared_preferences"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="SharedPreferences"        android:textAllCaps="false"        />    <Button        android:id="@+id/btn_file"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="File"        android:textAllCaps="false"        /></LinearLayout>

在这里插入图片描述


SharedPreferencesActivity以及对应的xml
Android之SharedPreferences轻量数据存储_第1张图片

package com.example.test0508.dataStorage;import androidx.appcompat.app.AppCompatActivity;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 com.example.test0508.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.edit_sp_name);        mBtnSave = findViewById(R.id.btn_sp_save);        mBtnShow = findViewById(R.id.btn_sp_show);        mTvContent = findViewById(R.id.tv_sp_content);        mSharedPreferences = this.getSharedPreferences("data",MODE_PRIVATE);        mEditor = mSharedPreferences.edit();        //两个按钮设置点击事件        mBtnSave.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //保存通过editor                mEditor.putString("name",mEtName.getText().toString());                //commit是同步存储 apply是异步存储                mEditor.apply();            }        });        mBtnShow.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //读取出来                mTvContent.setText(mSharedPreferences.getString("name",""));            }        });    }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="15dp"    >    <EditText        android:id="@+id/edit_sp_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入内容"        />    <Button        android:id="@+id/btn_sp_save"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="保存"        android:layout_marginTop="10dp"        />    <Button        android:id="@+id/btn_sp_show"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示"        android:layout_marginTop="10dp"        />    <TextView        android:id="@+id/tv_sp_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"    /></LinearLayout>

更多相关文章

  1. Android 向菜单按钮说再见
  2. android 按钮按下时改变字体颜色
  3. Android Activity sleep 自线程更新主线程UI
  4. Android 为什么主线程的looper 一直循环不会ANR
  5. Android按钮的点击效果的总结
  6. 改变Android按钮背景颜色的高效方法
  7. 修改Android actionbar 溢出菜单按钮(OverflowButton)默认图标
  8. Tab添加左右按钮

随机推荐

  1. 【4.5-4.11】上周精彩回顾
  2. hbase 学习
  3. 邮件加密的第三道坎是密钥管理
  4. 前端js是什么意思?
  5. Photoshop Elements:做出完美大合照,让笑容
  6. 我用注解实现了优雅的跨库查询,架构师竖起
  7. NA(Nirvana)公链“为应用而生” NAC公链
  8. Vsftpd虚拟用户登录
  9. 设计模式3之抽象工厂模式
  10. 这款手绘风格的在线制图软件超棒