1、SharedPreferences xml存储
xml布局文件
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入内容" />
<Button
android:id="@+id/button_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写数据" />
<Button
android:id="@+id/button_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读数据" />
在activity中对按钮建立点击事件
private void write() {
//写入数据的方法
SharedPreferences preferences = getSharedPreferences("preferences_write",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("edittext_input",mEdittext.getText().toString());//从edittext_input写入
editor.commit();
mEdittext.setText("");
}
private void read() {
//读数据的方法
SharedPreferences preferences = getSharedPreferences("preferences_write",MODE_PRIVATE);//文件名称要相同
String content = preferences.getString("edittext_input","没有写东西");//对应从edittext_input读出,"没有写东西"这是默认值
mTextview.setText(content);
}
2、内部存储
<Button
android:id="@+id/button_cache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入数据缓存到file" />