HarmonyOS 关于软键盘的相关问题

我想实现一个评论输入弹层,支持自定义表情键盘跟系统输入键盘切换功能,请看附件键盘反馈.mp4。目前是通过CustomDialog实现的,当前只实现了普通键盘输入功能,表情键盘暂时没有方案接入,需要看看是否有解决方案。

问题场景描述:

问题1:当键盘弹起时,中间那条缝怎么去掉?

问题2:当CustomDialog弹起键盘后,怎么自主控制键盘隐藏、显示,并且当键盘隐藏后dialog居于底部,而且输入框有焦点?

问题3:该怎么接入表情键盘,可以实现视频中的功能?

HarmonyOS
2024-08-08 17:58:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

请参考如下demo

//(1)注册窗口对键盘高度的监听 
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 window from '@ohos.window'; 
 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
    let window = windowStage.getMainWindowSync() 
    let storage: LocalStorage = new LocalStorage({ 
      'window': window 
    }); 
    windowStage.loadContent('pages/demo/car_home', storage, (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) ?? ''); 
    }); 
  } 
 
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
 
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
 
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
} 
//(2)页面的简化实现 
import window from '@ohos.window' 
import request from '@ohos.request' 
 
let storage = LocalStorage.getShared() 
 
@Entry(storage) 
@Component 
struct SheetTransitionExample { 
  @State text: string = "" 
  @State isShow:boolean = false 
  @State isShow2:boolean = false 
  @State sheetHeight:number = 470; 
  @State showDragBar:boolean = true; 
  @State isShow3:boolean = false 
  @State change: boolean = true 
  controller: TextAreaController = new TextAreaController() 
  window: window.Window = storage.get('window') 
 
  aboutToAppear(): void { 
    this.window.on('keyboardHeightChange', (keyboardHeight) => { 
      if (keyboardHeight == 0 && !this.isShow3) { 
        this.isShow = false 
      } 
    }) 
  } 
 
  @Builder myBuilder() { 
    Column() { 
      Row() { 
        TextArea({text: this.text, placeholder: "发条有爱的评论~", controller: this.controller}) 
          .width('80%') 
          .defaultFocus(true) 
          .key("input") 
          .onChange((value) => { 
            this.text = value 
          }) 
          .onFocus(() => { 
            this.change = true 
          }) 
      } 
      .height(50) 
      .justifyContent(FlexAlign.Start) 
      Row() { 
        Column() { 
          if (this.change) { 
            Button("表情") 
              .width('90%') 
              .onClick(() => { 
                this.isShow3 = true 
                this.change = false 
                focusControl.requestFocus('emoji') 
              }) 
          } else { 
            Button("键盘") 
              .width('90%') 
              .onClick(() => { 
                this.isShow3 = false 
                this.change = true 
                focusControl.requestFocus('input') 
              }) 
          } 
        }.width('25%') 
        Column() { 
          Button("b") 
            .width('90%') 
        }.width('25%') 
        Column() { 
          Button("c") 
            .width('90%') 
        }.width('25%') 
        Column() { 
          Button("d") 
            .width('90%') 
        }.width('25%') 
        Column() { 
          Button("e") 
            .width('90%') 
        }.width('25%') 
      } 
      .height(50) 
      .justifyContent(FlexAlign.Start) 
      .key('emoji') 
 
      if (this.isShow3) { 
        Row() { 
          Column() { 
            Text("a") 
              .width('90%') 
              .border({ 
                width: 2, 
                color: Color.Blue 
              }) 
              .onClick(() => { 
                this.text = this.text + "a" 
              }) 
          }.width('20%') 
          Column() { 
            Text("b") 
              .width('90%') 
              .border({ 
                width: 2, 
                color: Color.Blue 
              }) 
              .onClick(() => { 
                this.text = this.text + "b" 
              }) 
          }.width('20%') 
          Column() { 
            Text("c") 
              .width('90%') 
              .border({ 
                width: 2, 
                color: Color.Blue 
              }) 
              .onClick(() => { 
                this.text = this.text + "c" 
              }) 
          }.width('20%') 
          Column() { 
            Text("d") 
              .width('90%') 
              .border({ 
                width: 2, 
                color: Color.Blue 
              }) 
              .onClick(() => { 
                this.text = this.text + "d" 
              }) 
          }.width('20%') 
          Column() { 
            Text("e") 
              .width('90%') 
              .border({ 
                width: 2, 
                color: Color.Blue 
              }) 
              .onClick(() => { 
                this.text = this.text + "e" 
              }) 
          }.width('20%') 
        } 
        .height(350) 
        .width('100%') 
      } 
    } 
    .width('100%') 
    .height('100%') 
  } 
 
  build() { 
    Column() { 
      Button("点开评论") 
        .onClick(() => { 
          this.isShow = true 
        }) 
        .fontSize(20) 
        .margin(10) 
        .bindSheet($$this.isShow, this.myBuilder(), 
          { 
            height: this.sheetHeight, 
            dragBar: this.showDragBar, 
            showClose: false, 
            onAppear: () => { 
              console.log("BindSheet onAppear.") 
            }, onDisappear: () => { 
            console.log("BindSheet onDisappear.") 
            this.change = true 
          } 
          } 
        ) 
    } 
    .justifyContent(FlexAlign.Center) 
    .width('100%') 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-08-08 20:51:13
相关问题
关于软键盘弹出遮挡问题
1053浏览 • 1回复 待解决
CustomDialog与软键盘问题
288浏览 • 1回复 待解决
HarmonyOS 弹窗不避让软键盘
315浏览 • 1回复 待解决
HarmonyOS 如何代码控制软键盘弹出?
290浏览 • 1回复 待解决
HarmonyOS 如何获取软键盘高度?
244浏览 • 1回复 待解决
HarmonyOS如何代码收起软键盘
365浏览 • 1回复 待解决
如何判断软键盘是否弹出
1973浏览 • 1回复 待解决
HarmonyOS如何获取系统软键盘高度?
285浏览 • 1回复 待解决
如何主动收起软键盘
219浏览 • 1回复 待解决
API8 怎么隐藏软键盘
2424浏览 • 1回复 待解决
软键盘弹出时,页面的自适应
1308浏览 • 1回复 待解决
如何实现弹窗和软键盘避让
1310浏览 • 1回复 待解决
鸿蒙软键盘弹出后,页面底部按钮
3927浏览 • 0回复 待解决