电脑端窗口预关闭实现

电脑上点击窗口的关闭按钮,需要弹出窗口提示用户是否关闭

HarmonyOS
2024-05-26 16:06:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
wngsheng

使用的核心API

OnPrepareToTerminate(): boolean

此是UIAbility的一个接口,在UIAbility触发关闭时,进入回调,返回false表示正常关闭,返回true表示本次关闭取消。在这个回调中可以定义一些预操作,比如弹窗提示。

核心代码

在OnPrepareToTerminate这个回调中,可以通过自定义弹窗完成,也可以自己通过创建windows实现

import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import promptAction from '@ohos.promptAction'; 
 
 
export default class EntryAbility extends UIAbility { 
 
 
  onWindowStageCreate(windowStage: window.WindowStage) { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
 
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
 
 
  onPrepareToTerminate() { 
    this.showPrompt() 
    console.log("demoTest 进入回调") 
    //开发者定义预关闭 
    return true //确定停止关闭 
  } 
 
 
  showPrompt() { 
    try { 
      promptAction.showDialog({ 
        title: '是否关闭窗口', 
        buttons: [ 
          { 
            text: '确认', 
            color: '#000000', 
          }, 
          { 
            text: '取消', 
            color: '#000000', 
          }, 
        ], 
      }) 
        .then(data => { 
          console.info('showActionMenu success, click button: ' + data.index); 
          //目前是两个按钮 
          if (data.index == 0) { 
            //确认销毁 
            this.context.terminateSelf() 
          } 
          //取消不做处理 
          if (data.index == 1) { 
          } 
        }) 
        .catch(err => { 
          console.info('showActionMenu error: ' + err); 
        }) 
    } catch (error) { 
      console.error(`showActionMenu args error code is ${error.code}, message is ${error.message}`); 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

实现效果

点击关闭

弹出弹窗

分享
微博
QQ
微信
回复
2024-05-27 21:30:08


相关问题
如何通过代码关闭窗口
841浏览 • 1回复 待解决
ets开发关闭窗口组件问题
3856浏览 • 1回复 待解决
有32位电脑的鸿蒙系统组件吗?
8502浏览 • 3回复 待解决
HarmonyOS 关闭窗口前如何弹出提示?
445浏览 • 1回复 待解决
手机如何与电脑进行数据传输
3742浏览 • 1回复 待解决
HarmonyOS Tabs怎么实现加载
855浏览 • 1回复 待解决
如何实现page界面的渲染?
747浏览 • 1回复 待解决
使用dialog窗口实现红包雨窗口创建
1323浏览 • 1回复 待解决
Web组件中的加载,如何实现
2061浏览 • 1回复 待解决
电脑的应用
722浏览 • 1回复 待解决
全局关闭弹窗如何实现
974浏览 • 2回复 待解决