ArkUI的TextInput组件的customKeyboard使用案例?如何在customKeyboard中使用系统的安全键盘?

ArkUI的TextInput组件的customKeyboard使用案例?如何在customKeyboard中使用系统的安全键盘?

HarmonyOS
2024-06-05 21:29:56
717浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
day_night

1、主要使用半模态转场、焦点切换、输入控制三类能力

2、在实现的过程中注意切换各个控件的显示即可

3、当前实现方式主要以状态变量控制,可以适当优化代码逻辑,降低复杂度

以下是代码实现:

(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'); 
  } 
}
  • 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.

(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%') 
  } 
}
  • 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.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
分享
微博
QQ
微信
回复
2024-06-06 21:55:16
相关问题
customKeyboard属性使用问题
1197浏览 • 1回复 待解决
HarmonyOS TextInput组件使用
596浏览 • 1回复 待解决
HarmonyOS 如何在组件中使用临时变量?
1218浏览 • 1回复 待解决
HarmonyOS TextArea与系统键盘使用方式
610浏览 • 1回复 待解决
TextInputonSubmit事件如何使用
3197浏览 • 1回复 待解决
如何在Navigation中使用LocalStorage
1147浏览 • 1回复 待解决