HarmonyOS 鸿蒙Next 全局自定义弹窗的实现
HarmonyOS 鸿蒙Next 全局自定义弹窗的实现
更多关于HarmonyOS 鸿蒙Next 全局自定义弹窗的实现的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
楼主您好
可以参考demo
// CustomDialog_Globally.ets
import { BusinessError } from '[@ohos](/user/ohos).base';
export class Params {
text: string = “”
callback: (volumeType: string, value: number) => void = () => {
};
constructor(text: string, callback: (volumeType: string, value: number) => void ) {
this.text = text;
this.callback = callback;
}
}
@Builder
export function buildText(params: Params) {
// 弹窗内容
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
Button(“登录”)
.onClick(()=>{
params.text = ‘button 点击登录了’
// 登录结果回调
params.callback(‘1’, 2)
})
}.backgroundColor(Color.Yellow)
}
@Entry
@Component
struct CustomDialog_Globally {
@State message: string = “hello”
build() {
Row() {
Column() {
Button(“click me”)
.onClick(() => {
let uiContext = this.getUIContext();
let promptAction = uiContext.getPromptAction();
// let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));
try {
// promptAction.openCustomDialog(contentNode);
} catch (error) {
// let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
// console.error(OpenCustomDialog args error code is ${code}, message is ${message});
}
;
})
}
.width(‘100%’)
.height(‘100%’)
}
.height(‘100%’)
}
}
// CustomDialog_Globally_other01.ets
import { ComponentContent } from ‘@kit.ArkUI’;
import { BusinessError } from ‘@kit.BasicServicesKit’;
import { buildText, Params } from ‘./CustomDialog_Globally’;
@Entry
@Component
struct CustomDialog_Globally_other01 {
@State message: string = ‘Hello World’;
build() {
Column() {
Text(this.message)
.id(‘HelloWorld’)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
let uiContext = this.getUIContext();
let promptAction = uiContext.getPromptAction();
let callback = (volumeType: string, value: number) => {
//处理返回值的逻辑
console.log(OpenCustomDialog args error code is ${volumeType}, message is ${value});
this.message = volumeType;
}
let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message, callback));
try {
promptAction.openCustomDialog(contentNode);
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(OpenCustomDialog args error code is ${code}, message is ${message});
}
;
})
}
.height(‘100%’)
.width(‘100%’)
}
}
在HarmonyOS(鸿蒙)系统中实现全局自定义弹窗,通常涉及系统UI框架的定制与应用层交互。以下是一个简要的技术路径概述:
- 
权限与配置:确保应用已获得必要的系统权限,如显示系统级窗口权限。这通常需要在应用的
config.json文件中进行声明,并可能需要用户手动授予。 - 
使用Ability与UI组件:创建一个自定义的Ability,并在其布局文件中定义弹窗的UI组件。利用ArkUI(eTS或Java UI框架)进行界面设计。
 - 
窗口管理:通过系统提供的窗口管理服务(如WindowManager),将自定义弹窗以系统级窗口的形式显示。这可能涉及调用特定的API来设置窗口类型、层级和显示位置。
 - 
事件处理:为弹窗添加事件监听器,处理用户交互(如点击、滑动等)。确保弹窗的行为符合用户预期,并能在必要时正确关闭或更新内容。
 - 
兼容性测试:在不同设备、系统版本上进行测试,确保自定义弹窗的显示效果和功能一致性。
 
请注意,具体实现细节可能因鸿蒙系统的版本和API更新而有所变化。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html
        
      
                  
                  
                  
