String textSizeData = textSize.getText().toString();
String textColorData = textColor.getText().toString();
String contentData = content.getText().toString();
/**
* 获得本应用程序的SharedPreferences存储文件
* 参数1:文件名(如果文件名对应的文件不存在将自动创建
* data/data/(app_package)/shared_prefs/cont_info.xml 真机需要Root
* 参数2:文件的访问模式
MODE_PRIVATE 私有模式,只能本应用程序操作,会替换内容
MODE_APPEND 私有模式,追加内容
MODE_WORLD_READABLE 当前文件可以被其它应用读取
MODE_WORLD_WRITEABLE 当前文件可以被其它应用写入
*/
SharedPreferences sp = this.getSharedPreferences("cont_info",this.MODE_PRIVATE);
/** 开始保存数据 */
/** 1.获取一个编辑对象 */
SharedPreferences.Editor editor = sp.edit();
/** 写数据保存 */
editor.putString("textSizeData",textSizeData);
editor.putString("textColorData",textColorData);
editor.putString("contentData",contentData);
/** 提交编辑 */
editor.commit();
Toast.makeText(getApplication(),"保存成功", Toast.LENGTH_SHORT).show();
---获取数据
private EditText textSize,textColor,content;
textSize = findViewById(R.id.textSize);
textColor = findViewById(R.id.textColor);
content = findViewById(R.id.content);
/** 判断之前是否保存了数据 */
/** 获取SharedPreferences如果之前已经存在就直接获取 反之 创建一个新的文件对象 */
SharedPreferences sp = this.getSharedPreferences("cont_info",this.MODE_PRIVATE);
/**
* sp.getString("textSizeData","默认值")
* 去文件对象中的值
* 如果值不存在 返回默认值 “默认值”
* */
textSize.setText(sp.getString("textSizeData","20"));
textColor.setText(sp.getString("textColorData",null));
creater.setText(sp.getString("createrData",null));