Android简单数据存储类SharedPreferences详解及实例
2010-10-22 07:57:33 来源:WEB开发网sp.edit().putString("account",
etAccount.getText().toString()).commit();
sp.edit().putString("password",
etPW.getText().toString()).commit();
}
}
}
});
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(0);
}
});
tvClear.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {sp=getSharedPreferences("UserInfo", 0);
sp.edit().clear().commit();
}});
}
//初始化配置
private void InitConfig() {
sp = getSharedPreferences("UserInfo", 0);
etAccount.setText(sp.getString("account", null));
etPW.setText(sp.getString("password", null));
cbal.setChecked(sp.getBoolean("cbal", false));
cbrp.setChecked(sp.getBoolean("cbrp", false));
}
}
说明:
1.写内容
sp = getSharedPreferences("UserInfo", 0);
sp.edit().putBoolean("cbal", isChecked).commit();
UserInfo是指xml文件的文件名,如果此文件已存在则直接向其中写内容“isChecked”的值,首先通过SharedPreferences的edit()方法创建editor,然后调用commit()方法提修改
2.读内容
sp = getSharedPreferences("UserInfo", 0);
etAccount.setText(sp.getString("account", null));
etPW.setText(sp.getString("password", null));
cbal.setChecked(sp.getBoolean("cbal", false));
cbrp.setChecked(sp.getBoolean("cbrp", false));
赞助商链接