一、@ohos.hilog封装打印日志
作用:查错,相比打断点更快(打断点更细)
核心API:hilog
import { hilog } from "@kit.PerformanceAnalysisKit"
const domian =0x0000
const tag = 'mylog'
const format = '%{public}s'
export class Logger {
static info(...args:Object[]){
let newFormat = format.repeat(args.length)
args = args.map(item=>JSON.stringify(item,null,2))
hilog.info(domian,tag,newFormat,...args)
}
static debug(...args:Object[]){
let newFormat = format.repeat(args.length)
args = args.map(item=>JSON.stringify(item,null,2))
hilog.debug(domian,tag,newFormat,...args)
}
static warn(...args:Object[]){
let newFormat = format.repeat(args.length)
args = args.map(item=>JSON.stringify(item,null,2))
hilog.warn(domian,tag,newFormat,...args)
}
static error(...args:Object[]){
let newFormat = format.repeat(args.length)
args = args.map(item=>JSON.stringify(item,null,2))
hilog.error(domian,tag,newFormat,...args)
}
}
二、沉浸式显示模式
作用:全屏模式
核心API:window
import { window } from '@kit.ArkUI'
export class winManager{
static async enableFullScreen(){
const win = await window.getLastWindow(getContext())
win.setWindowLayoutFullScreen(true)
win.setSpecificSystemBarEnabled('navigationIndicator',false)
}
static async disableFullScreen(){
const win = await window.getLastWindow(getContext())
win.setWindowLayoutFullScreen(false)
}
static async getAvoidAreaTop(){
const win = await window.getLastWindow(getContext())
const area = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
let topHeight = px2vp(area.topRect.height)
AppStorage.setOrCreate('topHeight', topHeight)
}
static async getAvoidAreaBottom(){
const win = await window.getLastWindow(getContext())
const areaIndictor = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
let bottomHeight = px2vp(areaIndictor.bottomRect.height)
AppStorage.setOrCreate('bottomHeight', bottomHeight)
return bottomHeight
}
static async settingStatusBarContentColor(color:string){
const win = await window.getLastWindow(getContext())
win.setWindowSystemBarProperties({statusBarContentColor: color})
}
}
三、html文件加载
作用:可以打开html文件
核心API:webview
import { webview } from '@kit.ArkWeb'
@Entry
@Component
struct PreviewWebPage {
webController = new webview.WebviewController()
build() {
Navigation() {
Web({
src: $rawfile('index.html'), //这种不需要开启网络权限
controller: this.webController //3. 设置第1步实例化的控制器对象
})
}
.title('隐私政策')
.titleMode(NavigationTitleMode.Mini)
.height('100%')
.width('100%')
}
}
四、自定义弹窗 (CustomDialog)并且使用emitter跨线程通信
核心API:emitter
import { emitter } from '@kit.BasicServicesKit'
@CustomDialog
export struct HdLoadingDialog {
@Prop message: string = ''
controller: CustomDialogController
aboutToAppear(): void {
emitter.on({eventId:0},(rec)=>{
let data = rec.data!['percent'] as string
this.message = '上传:'+data
})
}
build() {
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
LoadingProgress().width(30).height(30).color('#fff')
if (this.message) {
Text(this.message).fontSize((14)).fontColor('#fff')
}
}
.width(150)
.height(50)
.padding(10)
.backgroundColor('rgba(0,0,0,0.5)')
.borderRadius(8)
}
}
五、用户首选项
核心API:preferences
用户首选项:用户首选项支持持久化轻量级数据,并提供相关方法对其新增、修改、查询、删除
// 1. 保存用户首选项中的关键
async saveKeyWord(keyword: string) {
// 1. 去用户首选项中获取key为keyword的数据,字符串数组
let pre = preferences.getPreferencesSync(getContext(), { name: 'store' })
// 2. 向首选项中获取数据
let kdArr = pre.getSync('keyword', []) as string[]
// 判断kdArr数组中如果已经存在了keyword变量的值,则不再追加
// ['html5','css3'] -> css3 --> 数组的 find,findindex,indexOf, includes
if (kdArr.includes(keyword)) {
return //不再追加
}
// 3. 利用push方法将用户输入的关键字追加到kdArr中
kdArr.push(keyword)
// 4. 利用putSync+flush方法完成写入
pre.putSync('keyword', kdArr)
await pre.flush()
}
// 2. 获取用户首选项中的关键字数组
async getKeyWords() {
let pre = preferences.getPreferencesSync(getContext(), { name: 'store' })
return pre.getSync('keyword', []) as string[]
}
// 3. 删除(① 删除指定关键字 ② 全部删除)
async delKeyWord(keywrod?: string) {
let pre = preferences.getPreferencesSync(getContext(), { name: 'store' })
if (keywrod) {
// 删除指定的关键字
// 1. 获取首选项的所有数据到内存中(字符串数组)
let kdArr = pre.getSync('keyword', []) as string[]
// 2. 根据已知的关键字keywrod 查找它在这个数组中的索引,同时调用splice删除之
let index = kdArr.findIndex(item => item == keywrod)
// ✨注意:如果 index < 0 则不删除
if (index < 0) {
promptAction.showToast({ message: '当前要删除掉关键字不存在' })
return
}
kdArr.splice(index, 1)
// 3. 调用 putSync将最新的数组写回文件
pre.putSync('keyword', kdArr)
} else {
// 删除全部
pre.deleteSync('keyword')
}
await pre.flush()
}