智能客服
你问我答,随时在线为你解决问题

























使用hitTestBehavior和setTimeout解决。示例代码如下:
- @Entry
- @Component
- struct ToggleDemo {
- @State isDarkMode: boolean = false;
- private timeoutID?: number;
-
- aboutToDisappear(): void {
- clearTimeout(this.timeoutID);
- }
-
- build() {
- Column() {
- Column() {
- Toggle({ type: ToggleType.Switch, isOn: $$this.isDarkMode })
- .onChange((isOn: boolean) => {
- console.info('Toggle.onChange:isOn' + isOn);
- this.isDarkMode = isOn;
- this.getUIContext().getHostContext()!.getApplicationContext().setColorMode(this.isDarkMode ? 0 : 1);
- })
- }
- // Set hitTestBehavior property to HitTestMode.Block to block Toggle component's event response.
- .hitTestBehavior(HitTestMode.Block)
- .onClick(() => {
- this.timeoutID = setTimeout(() => {
- this.isDarkMode = !this.isDarkMode;
- }, 1500);
- })
- }
- .width('100%')
- .height('100%')
- .padding(32)
- }
- }
智能客服
你问我答,随时在线为你解决问题
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功