一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

harmony-utils之WindowUtil,窗口相關(guān)工具類

童長(zhǎng)老 ? 來(lái)源:jf_14594073 ? 作者:jf_14594073 ? 2025-06-30 17:33 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

harmony-utils之WindowUtil,窗口相關(guān)工具類

harmony-utils 簡(jiǎn)介與說(shuō)明


[harmony-utils] 一款功能豐富且極易上手的HarmonyOS工具庫(kù),借助眾多實(shí)用工具類,致力于助力開(kāi)發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。其封裝的工具涵蓋了APP、設(shè)備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認(rèn)證、用戶首選項(xiàng)、拍照、相冊(cè)、掃碼、文件、日志,異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機(jī)、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開(kāi)發(fā)需求。
[picker_utils] 是harmony-utils拆分出來(lái)的一個(gè)子庫(kù),包含PickerUtil、PhotoHelper、ScanUtil。

下載安裝
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils

//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
   AppUtil.init(this.context);
 }

API方法與使用


setPreferredOrientation 設(shè)置窗口的顯示方向?qū)傩?/h5>
WindowUtil.setPreferredOrientation(window.Orientation.LANDSCAPE).then(() = > {
  ToastUtil.showToast(`設(shè)置成功!`)
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
getPreferredOrientation 獲取窗口的顯示方向?qū)傩?,主窗口調(diào)用
let orientation = WindowUtil.getPreferredOrientation();
 DialogHelper.showToast(`窗口屏幕方向:${orientation}`);
setWindowPrivacyMode 設(shè)置窗口是否為隱私模式。設(shè)置為隱私模式的窗口,窗口內(nèi)容將無(wú)法被截屏或錄屏
WindowUtil.setWindowPrivacyMode(true).then(() = > {
  ToastUtil.showToast("您已設(shè)置隱私模式,禁止截屏、錄像");
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
isPrivacyMode 窗口是否隱私模式,默認(rèn)主窗口
let isPrivacyMode = WindowUtil.isPrivacyMode();
 ToastUtil.showToast(`窗口是否隱私模式:${isPrivacyMode}`);
setWindowLayoutFullScreen 設(shè)置窗口的布局是否為沉浸式布局(該沉浸式布局狀態(tài)欄、導(dǎo)航欄仍然顯示)
WindowUtil.setWindowLayoutFullScreen(true).then(() = > {
  ToastUtil.showToast(`沉浸式布局已設(shè)置成功!`);
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
isLayoutFullScreen 判斷窗口是否為沉浸式,默認(rèn)主窗口
let isLayoutFullScreen = WindowUtil.isLayoutFullScreen();
 ToastUtil.showToast(`窗口是否為沉浸式:${isLayoutFullScreen}`);
setWindowSystemBarProperties 設(shè)置主窗口三鍵導(dǎo)航欄、狀態(tài)欄的屬性
WindowUtil.setWindowSystemBarProperties({
  statusBarColor: '#F00FF0',
  statusBarContentColor: '#0FF00F',
  isStatusBarLightIcon: true,
  navigationBarColor: '#F06060',
  navigationBarContentColor: "#0606F0",
  isNavigationBarLightIcon: true
}).then(() = > {
  ToastUtil.showToast("設(shè)置成功!");
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
getWindowSystemBarProperties 獲取主窗口三鍵導(dǎo)航欄、狀態(tài)欄的屬性
let properties = WindowUtil.getWindowSystemBarProperties();
   let jsonStr = JSON.stringify(properties, null, 2);
setImmersiveModeEnabledState 設(shè)置當(dāng)前窗口是否開(kāi)啟沉浸式布局,該調(diào)用不會(huì)改變窗口模式和窗口大小
WindowUtil.setImmersiveModeEnabledState(true);
getImmersiveModeEnabledState 查詢當(dāng)前窗口是否已經(jīng)開(kāi)啟沉浸式布局
let enabled = WindowUtil.getImmersiveModeEnabledState();
  ToastUtil.showToast(`是否開(kāi)啟沉浸式布局:${enabled}`);
setWindowGrayScale 設(shè)置窗口灰階。該接口需要在調(diào)用loadContent()或setUIContent()使窗口加載頁(yè)面內(nèi)容后調(diào)用。
WindowUtil.setWindowGrayScale(1.0);
setWindowBackgroundColor 設(shè)置窗口的背景色。Stage模型下,該接口需要在loadContent()或setUIContent()調(diào)用生效后使用
WindowUtil.setWindowBackgroundColor('#9932CC');
  ToastUtil.showToast("設(shè)置背景色成功!");
setWindowSystemBarEnable 設(shè)置主窗口三鍵導(dǎo)航欄、狀態(tài)欄、底部導(dǎo)航條的可見(jiàn)模式,狀態(tài)欄與底部導(dǎo)航條通過(guò)status控制、三鍵導(dǎo)航欄通過(guò)navigation控制
WindowUtil.setWindowSystemBarEnable(['status', 'navigation']).then(() = > {
  ToastUtil.showToast(`設(shè)置成功!`);
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
setSpecificSystemBarEnabled 設(shè)置主窗口三鍵導(dǎo)航欄、狀態(tài)欄、底部導(dǎo)航條的顯示和隱藏
WindowUtil.setSpecificSystemBarEnabled('navigationIndicator', true).then(() = > {
  ToastUtil.showToast(`設(shè)置成功!`);
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
setWindowKeepScreenOn 設(shè)置屏幕是否為常亮狀態(tài)
WindowUtil.setWindowKeepScreenOn(true).then(() = > {
  ToastUtil.showToast("你已設(shè)置常亮");
}).catch((err: BusinessError) = > {
  LogUtil.error(err);
});
isKeepScreenOn 屏幕是否常亮
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
 ToastUtil.showToast(`屏幕是否常亮:${isKeepScreenOn}`);
setWindowBrightness 設(shè)置屏幕亮度值
WindowUtil.setWindowBrightness(0.7).then(() = > {
  ToastUtil.showToast(`您已設(shè)置亮度!`);
}).catch((err: BusinessError) = > {
  LogUtil.error(`異常信息-code: ${err.code} - msg: ${err.message}`)
});
getBrightness 獲取屏幕亮度。該參數(shù)為浮點(diǎn)數(shù),可設(shè)置的亮度范圍為[0.0, 1.0],其取1.0時(shí)表示最大亮度值。如果窗口沒(méi)有設(shè)置亮度值,表示亮度跟隨系統(tǒng),此時(shí)獲取到的亮度值為-1
let brightness = WindowUtil.getBrightness();
 ToastUtil.showToast(`屏幕亮度:${brightness}`);
setWindowFocusable 設(shè)置使用點(diǎn)擊或其他方式使該窗口獲焦的場(chǎng)景時(shí),該窗口是否支持窗口焦點(diǎn)從點(diǎn)擊前的獲焦窗口切換到該窗口
WindowUtil.setWindowFocusable(true).then(() = > {
  ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
  ToastUtil.showToast("設(shè)置失敗!");
});
isFocusable 窗口是否可聚焦,默認(rèn)主窗口
let isFocusable = WindowUtil.isFocusable();
  ToastUtil.showToast(`窗口是否可聚焦:${isFocusable}`);
setWindowTouchable 設(shè)置窗口是否為可觸狀態(tài)
WindowUtil.setWindowTouchable(true).then(() = > {
  ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
  ToastUtil.showToast("設(shè)置失??!");
});
isTouchable 窗口是否可觸摸,默認(rèn)主窗口
let isTouchable = WindowUtil.isTouchable();
  ToastUtil.showToast(`窗口是否可觸摸:${isTouchable}`);
getWindowProperties 獲取當(dāng)前窗口的屬性,默認(rèn)主窗口
let properties = WindowUtil.getWindowProperties();
 let jsonStr = `${JSON.stringify(properties, null, 2)}`;
getWindowAvoidArea 獲取當(dāng)前應(yīng)用窗口內(nèi)容規(guī)避的區(qū)域。如系統(tǒng)欄區(qū)域、劉海屏區(qū)域、手勢(shì)區(qū)域、軟鍵盤區(qū)域等與窗口內(nèi)容重疊時(shí),需要窗口內(nèi)容避讓的區(qū)域
let area = WindowUtil.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
  let jsonStr = `${JSON.stringify(area, null, 2)}`;
getWindowType 獲取窗口類型,默認(rèn)主窗口
let windowType = WindowUtil.getWindowType();
getWindowStatus 獲取當(dāng)前應(yīng)用窗口的模式
let status = WindowUtil.getWindowStatus();
isFullScreen 判斷窗口是否全屏,默認(rèn)主窗口
let isFullScreen = WindowUtil.isFullScreen();
isFocused 判斷當(dāng)前窗口是否已獲焦
let isFocused = WindowUtil.isFocused();
isTransparent 窗口是否透明,默認(rèn)主窗口
let isTransparent = WindowUtil.isTransparent();
isWindowShowing 判斷當(dāng)前窗口是否已顯示,默認(rèn)主窗口
let isWindowShowing = WindowUtil.isWindowShowing();
isWindowSupportWideGamut 判斷當(dāng)前窗口是否支持廣色域模式,,默認(rèn)主窗口
let isWindowSupportWideGamut = await WindowUtil.isWindowSupportWideGamut();
setDialogBackGestureEnabled 設(shè)置模態(tài)窗口是否響應(yīng)手勢(shì)返回事件,非模態(tài)窗口調(diào)用返回錯(cuò)誤碼
WindowUtil.setDialogBackGestureEnabled(true).then(() = > {
  ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
  ToastUtil.showToast("設(shè)置失??!");
});
setGestureBackEnabled 設(shè)置當(dāng)前窗口是否禁用返回手勢(shì)功能,僅主窗全屏模式下生效,2in1設(shè)備下不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() = > {
  ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
  ToastUtil.showToast("設(shè)置失??!");
});
isGestureBackEnabled 獲取當(dāng)前窗口是否禁用返回手勢(shì)功能,僅主窗全屏模式下生效,2in1設(shè)備不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
 ToastUtil.showToast(`當(dāng)前窗口是否禁用返回:${isGestureBack}`);

創(chuàng)作不易,請(qǐng)給童長(zhǎng)老點(diǎn)贊

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • Harmony
    +關(guān)注

    關(guān)注

    0

    文章

    108

    瀏覽量

    3017
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    harmony-utilsCacheUtil,緩存工具

    harmony-utilsCacheUtil,緩存工具
    的頭像 發(fā)表于 07-04 16:36 ?121次閱讀

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的頭像 發(fā)表于 07-04 16:34 ?125次閱讀

    harmony-utilsCrashUtil,異常相關(guān)工具

    harmony-utilsCrashUtil,異常相關(guān)工具
    的頭像 發(fā)表于 07-04 16:33 ?123次閱讀

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具
    的頭像 發(fā)表于 07-03 18:27 ?160次閱讀

    harmony-utilsDisplayUtil,屏幕相關(guān)工具

    harmony-utilsDisplayUtil,屏幕相關(guān)工具
    的頭像 發(fā)表于 07-03 18:26 ?138次閱讀

    harmony-utilsEmitterUtil,Emitter工具

    harmony-utilsEmitterUtil,Emitter工具
    的頭像 發(fā)表于 07-03 18:24 ?138次閱讀

    harmony-utilsFileUtil,文件相關(guān)工具

    harmony-utilsFileUtil,文件相關(guān)工具
    的頭像 發(fā)表于 07-03 18:23 ?136次閱讀

    harmony-utilsFormatUtil,格式化工具

    harmony-utilsFormatUtil,格式化工具
    的頭像 發(fā)表于 07-03 18:22 ?139次閱讀

    harmony-utilsImageUtil,圖片相關(guān)工具

    harmony-utilsImageUtil,圖片相關(guān)工具
    的頭像 發(fā)表于 07-03 18:22 ?169次閱讀

    harmony-utilsLocationUtil,定位相關(guān)工具

    harmony-utilsLocationUtil,定位相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 07-03 18:13 ?142次閱讀

    harmony-utilsPreviewUtil,文件預(yù)覽工具

    harmony-utilsPreviewUtil,文件預(yù)覽工具 harmony-utils 簡(jiǎn)介與說(shuō)明 [
    的頭像 發(fā)表于 07-03 11:40 ?120次閱讀

    harmony-utilsSnapshotUtil,截圖相關(guān)工具

    harmony-utilsSnapshotUtil,截圖相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 07-03 11:36 ?115次閱讀

    harmony-utilsAuthUtil,生物認(rèn)證相關(guān)工具

    # harmony-utilsAuthUtil,生物認(rèn)證相關(guān)工具 ## harmony-utils
    的頭像 發(fā)表于 06-26 17:43 ?108次閱讀

    harmony-utilsNetworkUtil,網(wǎng)絡(luò)相關(guān)工具

    harmony-utilsNetworkUtil,網(wǎng)絡(luò)相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 06-25 23:46 ?49次閱讀

    harmony-utilsDateUtil,日期工具

    harmony-utilsDateUtil,日期工具
    的頭像 發(fā)表于 06-25 22:15 ?59次閱讀