HarmonyOS TextInput和键盘相关问题咨询

1、请问怎么手动使TextInput获取焦点,并弹出键盘?

2、怎么能控制输入框获取焦点时,页面上推?

3、怎么实现输入框输入的时候,点击其他区域,手动使输入框失焦,并关闭键盘?

HarmonyOS
2024-08-04 14:38:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
e_leaner

请参考如下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%') 
  } 
}

问题2:

bindSheet半模态页面不会避让键盘,sheetHeight的值设置为380时,半模态页面中输入框的位置就没有高于键盘的高度,所以键盘拉起时输入框被遮挡了部分。解决方案就是通过设置半模态页面的高度值+加上避让条的高度值,来解决输入框被遮住的问题。

windowStage.getMainWindow().then((win: window.Window) => { 
  win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); 
  win.on('avoidAreaChange', (data) => { 
    if (this.avoidType === data.type) { 
      Logger.warn(TAG, `avoidAreaChange Succeeded type=AvoidAreaType.TYPE_SYSTEM,area=${JSON.stringify(data.area)}`); 
      AppStorage.setOrCreate<number>(StorageKey.AVOID_STATUS_BAR_HEIGHT, data?.area?.topRect?.height || this.defaultStatusBarHeight); 
    } else if (this.navAvoidType === data.type) { 
      Logger.warn(TAG, `navAvoidAreaChange Succeeded type=AvoidAreaType.TYPE_NAVIGATION_INDICATOR,area=${JSON.stringify(data.area)}`); 
      AppStorage.setOrCreate<number>(StorageKey.AVOID_AI_GUIDE_BAR_HEIGHT, data?.area?.bottomRect?.height); 
    } 
  }); 
}).catch(() => { 
 
});

问题3:

在空白区域所属组件添加点击事件,通过focusControl.requestFocus('')将输入框组件上的焦点转移,方能实现组件失焦,键盘收起。

demo如下:

// xxx.ets

@Entry 
@Component 
struct TextInputExample { 
  @State message: string = '' 
  build() { 
    Column() { 
      Blank() 
        .width('100%') 
        .height('70%') 
      TextInput({placeholder: '请输入手机号'}) 
        .id('input') 
        .backgroundColor(Color.Pink) 
      Button('点击获焦') 
        .onClick(() =>{ 
          focusControl.requestFocus('input') 
        }) 
        .id('butt') 
    } 
    .onClick(() =>{ 
      focusControl.requestFocus('butt') 
    }) 
  } 
}
分享
微博
QQ
微信
回复
2024-08-05 12:11:01
相关问题
HarmonyOS 媒体问题相关咨询
94浏览 • 1回复 待解决
HarmonyOS AOT相关问题咨询
310浏览 • 1回复 待解决
HarmonyOS TextInput调用系统键盘问题
178浏览 • 1回复 待解决
jsBridge相关问题咨询
321浏览 • 1回复 待解决
HarmonyOS 音频播放相关问题咨询
100浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘问题
424浏览 • 1回复 待解决
HarmonyOS 媒体硬解相关问题咨询
466浏览 • 1回复 待解决
咨询下WebSocket相关问题
346浏览 • 1回复 待解决
HarmonyOS TextInput绑定自定义键盘问题
422浏览 • 1回复 待解决
奔溃解析相关问题咨询
32浏览 • 1回复 待解决
HarmonyOS 关于软键盘相关问题
346浏览 • 0回复 待解决
HarmonyOS 计步传感器相关问题咨询
83浏览 • 1回复 待解决
HarmonyOS InputText相关咨询
231浏览 • 1回复 待解决
设备唯一id相关问题咨询
1668浏览 • 1回复 待解决
重力传感器相关问题咨询
262浏览 • 1回复 待解决
HarmonyOS TextInput如何主动弹出键盘
228浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘
280浏览 • 1回复 待解决
HarmonyOS TextInput 换行问题
496浏览 • 1回复 待解决
HarmonyOS TextInput 组件问题
360浏览 • 1回复 待解决
HarmonyOS TextInput焦点问题
310浏览 • 1回复 待解决
AltBeacon在HarmonyOS下的相关实现咨询
267浏览 • 1回复 待解决
HarmonyOS卡片问题咨询
246浏览 • 1回复 待解决