HarmonyOS鸿蒙Next中RN项目RN中的Keyboard.addListener不生效

HarmonyOS鸿蒙Next中RN项目RN中的Keyboard.addListener不生效 大家好,想问下有没有人遇到过类似的问题?求帮忙分析看看,感谢!

在应用中当前textinput 点击无法拉起键盘,键盘监听事件未触发

原生需要有相应的键盘监听配置吗?

下面是关键代码:

cke_5815.png

cke_493.png


更多关于HarmonyOS鸿蒙Next中RN项目RN中的Keyboard.addListener不生效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

17 回复

我试了下这个demo,在“@react-native-oh/react-native-harmony: 0.72.27-1”下是可以正常拉起键盘的,RN也可以正常监听。

请问您有没有验证过这个demo。目前我的手机版本是6.0.0(API20),或者您的同事有高版本的测试机验证吗

更多关于HarmonyOS鸿蒙Next中RN项目RN中的Keyboard.addListener不生效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


我这个项目,里面RN所有的键盘监听都没有触发,

这个demo是从项目里面抽出来的,

老师您好,我发现你这个demo是关键代码,方便把这个demo的可运行全量代码贴出来吗。我去调试代码

当前crn版本下的鸿蒙工程中 oh_modules/@rnoh/react-native-openharmony/src/main/ets/RNOHCorePackage/turboModules/KeyboardObserverTurboModule.ts 文件下的代码与 “@react-native-oh/react-native-harmony”: "0.72.75"版本下当前文件的键盘KeyboardObserverTurboModule,文件差异比较大,且我这个版本下KeyboardObserverTurboModule中的断点没有触发,

@react-native-oh/react-native-harmony: 0.72.27-1

【解决方案】

开发者您好,可以参考以下Demo:

import React, { useEffect, useState } from 'react';
import { View, Text, TextInput, Button, StyleSheet, ScrollView, Keyboard } from 'react-native';
const KeyboardDemo = () => {
    const [keyboardStatus, setKeyboardStatus] = useState('hidden');
    const [keyboardHeight, setKeyboardHeight] = useState(0);
    const [inputText, setInputText] = useState('');
    const [logs, setLogs] = useState<string[]>([]);
    // 添加日志记录函数 
    const addLog = (message: string) => { setLogs(prev => [...prev.slice(-10), message]); };
    // 监听键盘事件 
    useEffect(
        () => { 
            const subscriptions = [
                Keyboard.addListener('keyboardWillShow', (e) => { addLog(`Keyboard WILL SHOW: ${JSON.stringify(e.endCoordinates)}`); setKeyboardStatus('will show'); }),
                Keyboard.addListener('keyboardDidShow', (e) => { addLog(`Keyboard DID SHOW: ${JSON.stringify(e.endCoordinates)}`); setKeyboardStatus('visible'); setKeyboardHeight(e.endCoordinates.height); }),
                Keyboard.addListener('keyboardWillHide', () => { addLog('Keyboard WILL HIDE'); setKeyboardStatus('will hide'); }),
                Keyboard.addListener('keyboardDidHide', () => { addLog('Keyboard DID HIDE'); setKeyboardStatus('hidden'); setKeyboardHeight(0); }),]; 
                return () => { subscriptions.forEach(sub => sub.remove()); }; 
            },[]);

    return (
        <View style={styles.container}> {/* 输入框触发键盘 */}
            <TextInput style={styles.input} placeholder="Tap here to open keyboard..." value={inputText} onChangeText={setInputText} />
            {/* 键盘状态信息 */}
            <View style={styles.infoBox}>
                <Text style={styles.text}> Keyboard Status: {keyboardStatus.toUpperCase()} </Text>
                <Text style={styles.text}> Keyboard Height: {keyboardHeight}px </Text>
                <Text style={styles.text}> Is Keyboard Visible: {Keyboard.isVisible() ? 'YES' : 'NO'} </Text>
            </View>
            {/* 操作按钮 */}
            <View style={styles.buttonGroup}>
                <Button title="Dismiss Keyboard" onPress={() => { Keyboard.dismiss(); addLog('Manually dismissed keyboard'); }} />
                <Button title="Check Visibility" onPress={() => { addLog(`Is visible: ${Keyboard.isVisible()}`); }} />
                <Button title="Get Metrics" onPress={() => { const metrics = Keyboard.metrics(); addLog(`Metrics: ${metrics ? JSON.stringify(metrics) : 'null'}`); }} />
            </View>
            <TextInput style={styles.input} placeholder="Tap here to open keyboard..." value={inputText} onChangeText={setInputText} />
            {/* 日志显示 */}
            <ScrollView style={styles.logsContainer}>
                {logs.map((log, index) => (<Text key={index} style={styles.logText}> {log} </Text>))}
            </ScrollView>
        </View>);
};

const styles = StyleSheet.create({
    container: {
        flex: 1
    },
    inner: {
        padding: 24,
        flex: 1,
        justifyContent: "space-around"
    },
    header: {
        fontSize: 36,
        marginBottom: 48
    },
    textInput: {
        height: 40,
        borderColor: "#000000",
        borderBottomWidth: 1,
        marginBottom: 36
    },
    btnContainer: {
        backgroundColor: "white",
        marginTop: 12
    }
});

export default KeyboardDemo;

若是不能解决您的问题,请提供以下信息:

  1. 最小复现demo。

  2. 开发工具、手机系统版本信息。

  3. 使用的@react-native-oh/react-native-harmony框架版本

这个方案试了没用,

键盘拉不起,原生是监听不到的,

有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html

这个项目中,能正常拉起键盘的地方,RN的监听事件都没有触发,

楼主手机软件版本(api版本)是多少?用的@react-native-oh/react-native-harmony框架版本是多少?

方便的话把代码贴出来,别只发截图

手机用的api13,

@react-native-oh/react-native-harmony: 0.72.27-1

crn版本较低可能,但是目前无法更换不了这个版本,牵一发而动全身,

在HarmonyOS Next中,RN项目的Keyboard.addListener失效问题源于鸿蒙系统架构变更。鸿蒙Next采用ArkTS作为主要开发语言,对RN的兼容性支持存在差异。Keyboard.addListener依赖的React Native键盘事件机制在鸿蒙Next中未完全适配。当前鸿蒙Next的键盘监听需使用ArkUI声明式开发范式,通过onKeyEvent或自定义弹窗组件实现软键盘监听。建议检查鸿蒙SDK版本是否支持RN键盘模块,并确认项目配置中已启用对应能力。

在HarmonyOS Next中,RN的Keyboard.addListener可能因系统差异或配置问题导致无法触发。请检查以下关键点:

  1. 权限与配置:确保应用已声明必要的输入法权限,并在module.json5中正确配置。例如:

    "requestPermissions": [
      {
        "name": "ohos.permission.GET_SYSTEM_ACCESSIBILITY"
      }
    ]
    
  2. 焦点管理:TextInput需正确获取焦点才能触发键盘。尝试在组件挂载后调用.focus()方法:

    useEffect(() => {
      inputRef.current?.focus();
    }, []);
    
  3. 生命周期时机:确认监听器在组件挂载时注册,并在卸载时移除:

    useEffect(() => {
      const subscription = Keyboard.addListener('keyboardDidShow', () => {});
      return () => subscription.remove();
    }, []);
    
  4. 系统兼容性:HarmonyOS Next可能对RN键盘事件的支持存在差异,建议测试物理设备而非模拟器。

若问题持续,可尝试使用HarmonyOS原生键盘监听API替代RN方案,通过Native Module桥接实现。

回到顶部