将用户的设置信息,以实体类为媒介,保存到内部存储或者SD卡的文件里。再次打开应用时,程序会自动调用文件中实体类,并对设置信息进行设置,使其恢复到用户最终保存的状态。
首先,创建一个实体类(这里要实现 Serializable 接口,即序列化,这样才能让类在内存中传递):
public class SettingData implements Serializable{
private boolean titleShow;
private String textSize;
private boolean notify;
private boolean plug;
private boolean autoRead;
private boolean transmit;
public SettingData() {
}
public SettingData(boolean titleShow, String textSize, boolean notify, boolean plug, boolean autoRead, boolean transmit) {
this.titleShow = titleShow;
this.textSize = textSize;
this.notify = notify;
this.plug = plug;
this.autoRead = autoRead;
this.transmit = transmit;
}
public boolean isTransmit() {
return transmit;
}
public void setTransmit(boolean transmit) {
this.transmit = transmit;
}
public boolean isTitleShow() {
return titleShow;
}
public void setTitleShow(boolean titleShow) {
this.titleShow = titleShow;
}
public String getTextSize() {
return textSize;
}
public void setTextSize(String textSize) {
this.textSize = textSize;
}
public boolean isNotify() {
return notify;
}
public void setNotify(boolean notify) {
this.notify = notify;
}
public boolean isPlug() {
return plug;
}
public void setPlug(boolean plug) {
this.plug = plug;
}
public boolean isAutoRead() {
return autoRead;
}
public void setAutoRead(boolean autoRead) {
this.autoRead = autoRead;
}
}
最后,就是Activity啦,其功能实现为设置好后点击右上角“完成”,即可保存设置内容。
方法一:通过SharedPreferences
,保存设置内容:
public class Setting2Activity extends AppCompatActivity {
private CheckBox titleShow,notify,plug,