1、SharedPreferences
A.sp介绍
保存少量的数据,且这些数据的格式非常简单。 存储5种原始数据类型: boolean, float, int, long, String
比如应用程序的各种配置信息(如是否打开音效、是否使用震动效果、小游戏的玩家积分等),记住密码功能,音乐播放器播放模式。
存哪了: /data/data/应用程序包名/shared_prefs/Xxx.xml文件,以Key-Value的格式存储
B.如何存储数据
步骤1:得到SharedPreferences对象 getSharedPreferences(“文件的名称”,“文件的类型”);
(1).Context.MODE_PRIVATE:指定该SharedPreferences数据只能被应用程序读写
(2)MODE_APPEND:检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
(3).Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他
应用程序读,但不能写。
(4).Context,MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序写,但不能读。
步骤2:得到 SharedPreferences.Editor编辑对象
SharedPreferences.Editor editor=sp.edit();
步骤3:添加数据
editor.putBoolean(key,value)
editor.putString()
editor.putInt()
editor.putFloat()
editor.putLong()
步骤4:提交数据 editor.commit()
写入数据
读取
删除
清空
输入框及时刷新
edt_mes.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>0){
but_send.setEnabled(true);
but_send.setBackgroundResource(R.drawable.but2_shape);
}else{
but_send.setEnabled(false);
but_send.setBackgroundResource(R.drawable.but_shape);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});