相亲网站上做绿叶的女人很多,做pc端网站怎么样,金诺网站建设,东莞网页模板建站官方文档#xff1a;通过用户首选项实现数据持久化 目录标题 使用场景第1步#xff1a;源码第2步#xff1a;启动模拟器第3步#xff1a;启动entry第6步#xff1a;操作样例2 使用场景
Preferences会将该数据缓存在内存中#xff0c;当用户读取的时候#xff0c;能够快…官方文档通过用户首选项实现数据持久化 目录标题 使用场景第1步源码第2步启动模拟器第3步启动entry第6步操作样例2 使用场景
Preferences会将该数据缓存在内存中当用户读取的时候能够快速从内存中获取数据当需要持久化时可以使用flush接口将内存中的数据写入持久化文件中。Preferences会随着存放的数据量越多而导致应用占用的内存越大因此Preferences不适合存放过多的数据也不支持通过配置加密适用的场景一般为应用保存用户的个性化设置字体大小是否开启夜间模式等。
第1步源码
import { common } from kit.AbilityKit;
import dataPreferences from ohos.data.preferences;Entry
Component
struct Index {State changeFontSize: number 16;// 上下文private context: common.UIAbilityContext getContext(this) as common.UIAbilityContext//1. 获取preferenceprivate preferencesInstance: dataPreferences.Preferences dataPreferences.getPreferencesSync(this.context, { name: myStore });aboutToAppear(): void {//4. 页面打开后直接从preference中获取上一次的数据let result this.preferencesInstance.getSync(fontSizeKey, 16)this.changeFontSize Number(result)}build() {Column() {Row({ space: 10 }) {Text(当前进度一览).fontSize(this.changeFontSize)}.margin(20)Slider({value: this.changeFontSize,min: 14,max: 22,step: 2,style: SliderStyle.InSet}).showSteps(true).width(75%).onChange(async (value: number) {this.changeFontSize value//2. 保存数据this.preferencesInstance.putSync(fontSizeKey, this.changeFontSize);//3. 持久化数据this.preferencesInstance.flush()})}.backgroundColor(#f2f3f5).width(100%).height(100%)}
}
第2步启动模拟器 第3步启动entry 第6步操作 样例2
import dataPreferences from ohos.data.preferences;
import { common } from kit.AbilityKit;Entry
Component
struct Index_preferences2 {State message: string Hello World;private context:common.UIAbilityContext getContext(this) as common.UIAbilityContextprivate preferencesInstance: dataPreferences.Preferences dataPreferences.getPreferencesSync(this.context, { name: myStore });aboutToAppear(): void {let result this.preferencesInstance.getSync(messageKey,默认值1)this.message String(result)}build() {Row() {Column() {TextInput({text:this.message}).fontSize(20).fontWeight(FontWeight.Bold).onChange((value){this.message value})Button(保存).onClick((){this.preferencesInstance.putSync(message, this.message);this.preferencesInstance.flush()AlertDialog.show({message:保存成功})})Button(读取).onClick(() {let result this.preferencesInstance.getSync(messageKey,默认值2)this.message String(result)//获取到的数据不是String需要转换一下AlertDialog.show({message:this.message})console.log(test,result)})}.width(100%)}.height(100%)}
} 文章转载自: http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.kaakyy.com.gov.cn.kaakyy.com http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.rgrdd.cn.gov.cn.rgrdd.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn