HarmonyOS TextInput/TextArea如何设置键盘上方的自定义视图?

HarmonyOS
2024-12-24 17:06:20
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect
import { window } from '@kit.ArkUI';
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct Index {
  @State message: string = '图文信息区域\n\n图文信息区域\n\n图文信息区域'; // 第一层文本区
  @State isVisible: Visibility = Visibility.Hidden;
  @State keyboardHeight: string = '0px'; // 直接监听即可,不用预先设置

  controller: RichEditorController = new RichEditorController();

  aboutToAppear(): void {
    window.getLastWindow(getContext(this)).then((win) => {
      win.on("keyboardHeightChange", (height) => {
        console.log('height' + height.toString())
        if (height) {
          this.keyboardHeight = height.toString() + 'px';
          this.isVisible = Visibility.Visible;
        } else {
          this.isVisible = Visibility.Hidden;
        }
      })
    })
  }

  build() {
    Stack({ alignContent: Alignment.Bottom}) {
      Column() {
        Text(this.message)
          .fontSize(52)
          .height('90%')
          .width('100%')
          .textAlign(TextAlign.Center)
          .fontColor(Color.Black)
        Row() {
          Image($r('app.media.comment'))
            .width('50%')
            .margin(15)
            .onClick(() => {
              this.isVisible = Visibility.Visible;
              setTimeout(() => {
                focusControl.requestFocus('textArea');
              }, 10)
            })

          Image($r('app.media.like'))
            .width('12%')
            .margin(10)
          Image($r('app.media.collect'))
            .width('15%')
            .margin(10)
        }
        .height('10%')
        .width('100%')
      }
      .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM])

      // 遮罩
      Column()
        .width('100%')
        .height('100%')
        .opacity(0.5)
        .height('100%')
        .backgroundColor('rgba(0,0,0,0.5)')
        .onClick(() => {
          this.isVisible = Visibility.Hidden;
        })
        .visibility(this.isVisible)
        .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM, SafeAreaEdge.TOP])

      CommentComponent({isVisible: this.isVisible, keyboardHeight: this.keyboardHeight})
    }
  }

  aboutToDisappear(): void {
    this.isVisible = Visibility.Hidden;
  }
}

@Component
struct CommentComponent {
  controller: RichEditorController = new RichEditorController();
  @Link isVisible: Visibility;
  @Link keyboardHeight: string;

  build() {
    // 评论区自定义组件
    Flex({ direction: FlexDirection.ColumnReverse, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Stretch }) {

      Row() {
        RichEditor({ controller: this.controller })
          .id('textArea')
          .placeholder('喜欢就评论一下吧~', {
            fontColor: Color.Gray,
            font: {
              size: 16,
              weight: FontWeight.Normal
            }
          })
          .width('65%')

        Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.End, alignItems: ItemAlign.Stretch}) {
          Image($r('app.media.at')).height(20)
            .constraintSize({ minWidth: 20, maxWidth: 20})

          Image( $r('app.media.emoji')).height(20)
            .constraintSize({ minWidth: 20, maxWidth: 20})

          Image($r('app.media.cameraPicker'))
            .constraintSize({ minWidth: 20, maxWidth: 20})
            .height(20)
        }
        .width(120)
      }
      .margin({
        top: 10,
        bottom: this.keyboardHeight,
        left: 10,
        right: 10
      })
      .border({ width: 1})
      .borderRadius(20)
    }
    .width('100%')
    .height(380)
    .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM, SafeAreaEdge.TOP])
    .backgroundColor(Color.White)
    .visibility(this.isVisible)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
分享
微博
QQ
微信
回复
2024-12-24 18:53:47
相关问题
HarmonyOS TextInput自定义键盘
1307浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘问题
1562浏览 • 1回复 待解决
HarmonyOS TextInput绑定自定义键盘问题
1365浏览 • 1回复 待解决
HarmonyOS 自定义键盘
1023浏览 • 1回复 待解决
基于自定义键盘设置光标位置
1366浏览 • 1回复 待解决
HarmonyOS 数字自定义键盘如何实现
1282浏览 • 1回复 待解决
TextInput ,TextArea无法设置字体间距
965浏览 • 1回复 待解决