@CustomDialog @Component struct CustomDialogForSolutionOne { @Link textValue: string; controller?: CustomDialogController; build() { Column({ space: 20 }) { Text('自定义弹窗').fontSize(20); TextInput({ placeholder: '', text: this.textValue }) .height(50) .width('90%') .onChange((value: string) => { this.textValue = value; }) // 弹窗显示时通过defaultFocus设置默认焦点 .defaultFocus(true); } .justifyContent(FlexAlign.Center) .borderRadius(20) .padding(16) .backgroundColor(Color.White); } } @Entry @Component struct CustomDialogDemo { @State textValue: string = ''; solutionOneController: CustomDialogController | null = new CustomDialogController({ builder: CustomDialogForSolutionOne({ textValue: this.textValue, }), autoCancel: true, alignment: DialogAlignment.Bottom, customStyle: true, }); // 在自定义组件即将析构销毁时将dialogController置空 aboutToDisappear() { // 将dialogController置空 this.solutionOneController = null; } build() { Column({ space: 20 }) { Button('defaultFocus默认焦点') .backgroundColor('#0A59F7') .type(ButtonType.ROUNDED_RECTANGLE) .onClick(() => { if (this.solutionOneController != null) { this.solutionOneController.open(); } }); } .justifyContent(FlexAlign.Center) .height('100%') .width('100%'); } }
效果图如下:

@Entry @Component struct SearchFocus { @State @Watch('search') flag: boolean = false; @State changeValue: string = ''; @State submitValue: string = ''; @State backColor: string | Color = ''; @State isFocus: string = ''; search() { console.info(`flag: ${this.flag}`); try { focusControl.requestFocus('search'); } catch (error) { console.error(`error: ${error}`); } } build() { Column({ space: 15 }) { Row() { SearchComponent({ changeValue: this.changeValue, submitValue: this.submitValue, backColor: this.backColor, isFocus: this.isFocus }) .visibility(this.flag ? Visibility.Visible : Visibility.Hidden); } .width('100%') .height(80) .justifyContent(FlexAlign.Center); Text(this.isFocus); Row() { Button('打开search') .onClick(() => { this.flag = true; }); Button('关闭search') .onClick(() => { this.flag = false; this.getUIContext().getFocusController().clearFocus(); }); } .width('100%') .justifyContent(FlexAlign.SpaceAround); } .width('100%') .height('100%'); } } @Component struct SearchComponent { @Link changeValue: string; @Link submitValue: string; @Link backColor: string | Color; @Link isFocus: string; build() { Search({ placeholder: '请输入' }) .searchButton('搜索') .width('90%') .height(40) .backgroundColor(this.backColor) .placeholderColor('#ccc') .onSubmit((value: string) => { this.submitValue = value; }) .onChange((value) => { this.changeValue = value; }) .onFocus(() => { this.backColor = '#f1f3f5'; this.isFocus = '已获焦'; console.info(`onFocus已获焦`); }) .onBlur(() => { this.backColor = Color.Blue; this.isFocus = '未获焦'; console.info(`onBlur未获焦`); }) .id('search') .margin(20); } }
效果图如下:


@Entry @Component struct TextInputFocusExample { controller: TextInputController = new TextInputController(); dialogController: CustomDialogController = new CustomDialogController({ builder: CustomDialogFocusExample({ cancel: () => { this.onCancel(); }, confirm: () => { this.onAccept(); }, }), }); onCancel() { } onAccept() { } build() { Column() { TextInput({ placeholder: '请输入用户名', controller: this.controller }) .height(40) .margin(20) .fontSize(14) .width('90%') .onFocus(() => { console.info('TextInput获焦'); }) .onBlur(() => { console.info('TextInput失焦'); }); Button('弹出Dialog') .backgroundColor('#0a59f7') .id('Button') .onClick(() => { try { this.getUIContext().getFocusController().clearFocus(); } catch (error) { console.error(`error: ${error}`); } this.dialogController.open(); }) .width('30%'); } .width('100%') .height('100%'); } } @CustomDialog struct CustomDialogFocusExample { cancel?: () => void; confirm?: () => void; controller: CustomDialogController; build() { Column() { Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 }); Flex({ justifyContent: FlexAlign.SpaceAround }) { Button('关闭') .onClick(() => { this.controller.close(); if (this.cancel) { this.cancel(); } }) .backgroundColor(Color.White) .fontColor(Color.Black); Button('确定') .onClick(() => { this.controller.close(); if (this.confirm) { this.confirm(); } }) .backgroundColor('#0a59f7') .fontColor(Color.White); } .margin({ bottom: 10 }); }; } }
效果图如下:
