SharedPreferences储存
是我们安卓五大储存方式之一,主要用于储存少量数据
实现步骤:
//清空
private void bt_clear() {
//获取SP对象
SharedPreferences wz = getSharedPreferences("WZ", MODE_PRIVATE);
//获取编辑器
SharedPreferences.Editor edit = wz.edit();
//清空数据
edit.clear();
//发送数据
edit.commit();
}
//删除
private void bt_ramove() {
//获取SP对象
SharedPreferences wz = getSharedPreferences("WZ", MODE_PRIVATE);
//获取编辑器
SharedPreferences.Editor edit = wz.edit();
//删除数据
edit.remove("name");
//发送数据
edit.commit();
}
//查询
private void bt_read() {
//获取SP对象
SharedPreferences wz = getSharedPreferences("WZ", MODE_PRIVATE);
//获取数据
String name = wz.getString("name", "弱智");
String sex = wz.getString("sex", "女");
Toast.makeText(this, name+sex, Toast.LENGTH_SHORT).show();
}
//写入
@SuppressLint("CommitPrefEdits")
private void bt_writer() {
//获取SP对象
SharedPreferences wz = getSharedPreferences("WZ", MODE_PRIVATE);
//获取编辑器
SharedPreferences.Editor edit = wz.edit();
//放入数据
edit.putString("name","王震");
edit.putInt("age",17);
edit.putString("sex","男");
//发送数据
edit.commit();
}
TextWatcher监听
是监听editText实时内容长度的监听
实现步骤:
editText.addTextChangedListener(new TextWatcher() {
//改变前
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
//改变时
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0){
button.setEnabled(true);
button.setBackgroundResource(R.drawable.by_2);
}else {
button.setBackgroundResource(R.drawable.by_1);
button.setEnabled(false);
}
}
//改变后
@Override
public void afterTextChanged(Editable s) {
}
});