- 最后生成xml文件 k-v形式
- SharedPreferences
- 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>