android中数据存储主要分为三类:
sharedpreferences也是可以说是内部存储的一种方式,但是相比于内部存储使用更加便捷,适合在保存用户偏好设置时使用。本文将介绍sharedpreferences的使用方法。
使用步骤:
- 拿到sharedPreferences: this.getSharedPreferences(“settings_info”, MODE_PRIVATE);
- 进入edit模式,拿到编辑器:SharedPreferences.Editor edit=mSharedPreferences.edit();
- 保存数据【有多种数据类型可以保存:boolean,string,int,float等】:edit.putBoolean(“state”,isChecked);//以键值对的形式保存/读取数据
- 提交编辑器:edit.commit();
sharedpreferences的存储路径:/data/data/shared_prefs目录下
——————————————————————————————————————————
案例1:用户偏好设置
效果:更改了选项后再次回到该界面不会恢复默认值
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未知来源"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:padding="20dp"/>
<TextView
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="允许安装未知来源的应用"
android:textSize="18sp"/>
</LinearLayout>
<Switch
android:id="@+id/is_allow_unknown_apps_sources_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"/>
</RelativeLayout>
public class PreferenceDemoActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private static final String TAG ="PreferenceDemoActivity" ;
private Switch mIsAllowUnknownSource;
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preference_demo);
mIsAllowUnknownSource = findViewById(R.id.is_allow_unknown_apps_sources_switch);
mIsAllowUnknownSource.setOnCheckedChangeListener(this); //当选择改变时的监听
mSharedPreferences = PreferenceDemoActivity.this.getSharedPreferences("settings_info", MODE_PRIVATE);
//回显数据,默认为false
boolean state = mSharedPreferences.getBoolean("state", false);
mIsAllowUnknownSource.setChecked(state);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//假设说在这里需要对数据进行保存,并设置回显
Log.d(TAG,"current state is "+isChecked);
SharedPreferences.Editor edit=mSharedPreferences.edit();
edit.putBoolean("state",isChecked);
edit.commit();
}
}
文件路径:
内容:
案例2:数据存储、读取展示
存数据时使用:put
读数据时使用:set
效果:输入完内容后可以将数据内容展示,可设置,在退出应用程序后再次打开仍回显数据(使用场景:登录时的记住密码,记住账号等功能)
<?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">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input message"
android:textSize="20sp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="存数据"/>
<Button
android:id="@+id/btn_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读数据"/>
<TextView
android:id="@+id/tv_show_message"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#60999999"
android:textSize="20sp"/>
</LinearLayout>
public class SharedActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG ="SharedActivity" ;
private Button mBtnWrite;
private Button mBtnRead;
private EditText mEtMessage;
private TextView mTvShow;
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared);
initView();
initEvent();
mSharedPreferences = this.getSharedPreferences("message_info", MODE_PRIVATE);
mTvShow.setText(mSharedPreferences.getString("message","null"));
}
private void initEvent() {
mBtnWrite.setOnClickListener(this);
mBtnRead.setOnClickListener(this);
}
private void initView() {
mBtnWrite = findViewById(R.id.btn_save);
mBtnRead = findViewById(R.id.btn_read);
mEtMessage = findViewById(R.id.et_input);
mTvShow = findViewById(R.id.tv_show_message);
}
@Override
public void onClick(View v) {
if (v == mBtnWrite){
Log.d(TAG,"write message");
String message = mEtMessage.getText().toString();
SharedPreferences.Editor edit = mSharedPreferences.edit();
edit.putString("message",message);
edit.commit();
}else if (v == mBtnRead){
Log.d(TAG,"read message...");
mTvShow.setText(mSharedPreferences.getString("message","null"));
}
}
}