文档开发者中心
我的
|

UI

HarmonyOS 5

TextArea获取到焦点时,软键盘未能将输入框顶起来

聊天页面中有个emoji表情面板,当表情面板显示的时候点击输入框,通过对监听
收起
自动换行
深色代码主题
复制
.onBlur和
收起
自动换行
深色代码主题
复制
.onEditChange方法,发现TextArea已经获取到了焦点,但是软键盘未能将输入框顶起来,代码如下:
收起
自动换行
深色代码主题
复制
TextArea({ controller: this.controller, text: this.content })
  .borderRadius(6)
  .layoutWeight(1)
  .backgroundColor('#F7F7F7')
  .onBlur(()=>{
    LogUtil.info('---onBlur---')
  })
  .onEditChange((isEdit: boolean) => {
    if(isEdit){
      this.showAddPanel = false
      this.showEmoji = false
      this.showRecordVoice = false
    }
  })
  .maxLines(0)
  .onChange((value) => {
    this.content = value
    this.showSendBtn = value.trim().length > 0
  })
点赞
收藏
回复
2
分享
举报
浏览113 发布于2026-01-14 02:37山东
全部评论
最多点赞
最新发布
最早发布

自行处理键盘避让问题,将页面设置为不避让键盘,然后监听键盘高度,给输入框部分增加padding/margin为键盘高度。

收起
自动换行
深色代码主题
复制
/**
 * @fileName : EmotionView.ets
 * @author : @cxy
 * @date : 2025/12/19
 * @description : 表情面板示例
 */
import { EmotionComponent } from "./EmotionComponent"
import { Emotion } from "./EmotionImage"
import { window } from "@kit.ArkUI"
import { KeyboardAvoidMode } from '@kit.ArkUI';

enum ShowType {
  None = 0,
  Text = 1,
  Emotion = 2
}

@Component
export struct EmotionView {
  @State enableSend: boolean = false
  @State showType: ShowType = ShowType.None
  @State keybordHeight: number = 314
  @State bottomSafeHeight: number = 34 //TODO:监听avoidAreaChange获取
  private controller: RichEditorController = new RichEditorController()

  aboutToAppear(): void {
    const context = this.getUIContext().getHostContext()
    if (context) {
      this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.NONE)

      window.getLastWindow(context).then((win) => {
        win.setWindowLayoutFullScreen(true)
        win.on('keyboardHeightChange', this.keyboardHeightChange)
      })
    }
  }

  aboutToDisappear(): void {
    const context = this.getUIContext().getHostContext()
    if (context) {
      window.getLastWindow(context).then((win) => {
        win.off('keyboardHeightChange', this.keyboardHeightChange)
      })
    }
  }

  build() {
    Column() {
      Row({ space: 10 }) {
        RichEditor({
          controller: this.controller
        })
          .id('richEditorId')
          .backgroundColor('#f0f0f0')
          .borderRadius(6)
          .onEditingChange((isEditing) => {
            if (isEditing) {
              this.showType = ShowType.Text
            } else {
              if (this.showType === ShowType.Text) {
                this.showType = ShowType.None
              }
            }
          })
          .onDidChange((rangeBefore: TextRange, rangeAfter: TextRange) => {
            this.enableSend = this.controller.getSpans().length > 0
          })
          .layoutWeight(1)

        Text(this.showType === ShowType.Emotion ? '文本' : '表情')
          .fontSize(20)
          .onClick(() => {
            if (this.showType === ShowType.Emotion) {
              this.showType = ShowType.Text
              focusControl.requestFocus('richEditorId');
            } else {
              this.controller.stopEditing()
              this.showType = ShowType.Emotion
            }
          })
      }
      .alignItems(VerticalAlign.Center)
      .padding(10)

      EmotionComponent({
        emotionHeight: this.keybordHeight,
        enableSend: this.enableSend,
        onSelectEmoji: (e: string) => {
          this.controller.addTextSpan(e)
        },
        onDeleteEmoji: () => {
          // TODO: 删除
          // this.controller.deleteSpans()
        },
        onSendEmotion: (e?: Emotion) => {
          console.dir(e)
        }
      })
        .visibility(this.showType === ShowType.Emotion ? Visibility.Visible : Visibility.None)
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.End)
    .padding({
      bottom: this.getBottomHeight()
    })

  }

  getBottomHeight(): number {
    if (this.showType === ShowType.Text) {
      return this.keybordHeight
    }
    if (this.showType === ShowType.Emotion) {
      return 0
    }
    return this.bottomSafeHeight //底部安全区域高度,
  }

  private keyboardHeightChange = (data: number) => {
    if (data > 0) {
      this.keybordHeight = this.getUIContext().px2vp(data)
    }
  }
}

可以参考这篇自定义表情包键盘

展开全部
2楼回复于2026-01-15 08:37 来自四川

这里有一个官方Demo,实现了软键盘、表情输入框等,希望可以帮到你!

cke_665.png

项目地址:https://gitcode.com/HarmonyOS_Samples/CommentReply

1楼回复于2026-01-14 06:09 来自北京
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。