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

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(安卓)Activity sleep 自线程更新主线程UI
  2. Android(安卓)为什么主线程的looper 一直循环不会ANR
  3. android中Json数据保存方式
  4. Android(安卓)数据存储与读取:文件
  5. 关于Android中的消息机制和异步
  6. android的文件操作
  7. Android(安卓)Develop Training——Android保存应用配置(Saving
  8. (转)Android内存管理机制之一:lowmemory killer
  9. Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)

随机推荐

  1. Android(安卓)反编译apk文件得到项目文件
  2. 【Java/Android性能优5】 Android(安卓)I
  3. Android 串口通信
  4. Android开源测试框架
  5. Failed resolution of: Lcom/growingio/a
  6. 基于Win10、Cordova9.0.0的Android开发环
  7. Android基本界面元素的使用与讲解
  8. Android安卓开发知识库汇总
  9. Android(安卓)Adapter使用总结
  10. Android系统架构剖析