五笔打字通主页
媒体查询之系统深浅色的监控判断,代码如下:
import { mediaquery } from '@kit.ArkUI'; //导入媒体查询模块
@Entry
@Component
struct Page8 {
@State message: string = '';
@State backColor: Color = Color.White
//通过 matchMediasync 接口设置媒体查询条件 - wb98.com
private listener0: mediaquery.MediaQueryListener =
this.getUIContext().getMediaQuery().matchMediaSync('(dark-mode: true)')//true为深色,false为浅色
aboutToAppear(): void {
//所有监听绑定事件
this.listener0.on('change', (result: mediaquery.MediaQueryResult) => {
if (result.matches) {
this.backColor = Color.Blue
this.message = '深色模式'
} else {
this.backColor = Color.Yellow
this.message = '浅色模式'
}
})
}
aboutToDisappear(): void {
//解绑监听
this.listener0.off('change')
}
build() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Medium)
}
.width('100%')
.height('100%')
.backgroundColor(this.backColor)
}
}来源:济亨网
本文链接:https://wb98.com/post/382.html