例如下图,播放暂停按钮对应着两种状态,在状态切换时需要实时变化对应的标注信息。

import { PromptAction } from "@kit.ArkUI"
const RESOURCE_STR_PLAY: Resource = $r('sys.media.ohos_ic_public_play');
const RESOURCE_STR_PAUSE: Resource = $r('sys.media.ohos_ic_public_pause');
@Entry
@Component
export struct Rule_2_1_8 {
title: string = 'Rule 2.1.8';
@State isPlaying: boolean = true;
uiContext: UIContext = this.getUIContext();
promptAction: PromptAction = this.uiContext.getPromptAction();
play() {
console.info('play audio file');
}
pause() {
console.info('pause playing of audio file');
}
build() {
NavDestination() {
Column() {
Flex({
direction: FlexDirection.Column,
alignItems: ItemAlign.Center,
justifyContent: FlexAlign.Center,
}) {
Row() {
Image(this.isPlaying ? RESOURCE_STR_PAUSE : RESOURCE_STR_PLAY)
.width(50)
.height(50)
.onClick(() => {
this.promptAction.showToast({
message :this.isPlaying ? 'Play' : 'Pause'
})
this.isPlaying = !this.isPlaying;
if (this.isPlaying) {
this.play();
} else {
this.pause();
}
})
.accessibilityText(this.isPlaying ? 'Pause' : 'Play') // 设置可访问性框架的注释信息。
}
}
.width('100%')
.height('100%')
.backgroundColor(Color.White)
}
}.title(this.title)
}
}