React Native开发鸿蒙Next---RN键盘问题

276 阅读1分钟

React Native开发鸿蒙Next---RN键盘问题

应用第一版开发的时候提到过RN下键盘不响应的问题:react-native-keyboard-aware-scroll-view无法处理键盘遮挡。调查了下是键盘的回调不响应。

现在硬是熬到了问题解决。

某次鸿蒙Next版本更新后,现在HarmonyRN下的ScrollView会自动去处理键盘遮挡组件了。RN里的Keyboard组件依然无法收到各种监听,但可以执行dismiss。

无话可说,可以用下面的代码测试下,基于react-native-keyboard-aware-scroll-view的demo。

import React, {useState, useRef, useEffect} from 'react';
import ReactNative, {
  TextInput,
  StyleSheet,
  Button,
  View,
  Text,
  ScrollView,
  Keyboard
} from 'react-native';
import {KeyboardAwareScrollView} from '@react-native-oh-tpl/react-native-keyboard-aware-scroll-view';
const Labels = [
  'Username',
  'Password',
  'Firstname',
  'Lastname',
  'Address',
  'Phone',
  'Email',
  'QQ',
  'WeChat',
  'Webo',
];
// function Test(): JSX.Element {
export const Test: React.FC = (): JSX.Element => {
  const [resetScrollToCoords, setResetScrollToCoords] = useState({
    x: 20,
    y: 20,
  });
  const [enableAutomaticScroll, setEnableAutomaticScroll] = useState(false);
  const [extraHeight, setExtraHeight] = useState(50);
  const [extraScrollHeight, setExtraScrollHeight] = useState(60);
  const [enableResetScrollToCoords, setEnableResetScrollToCoords] =
    useState(false);
  /**
   * 模拟componentDidMount,即只运行一次该函数
   */
  useEffect(() => {
    // | 'keyboardWillShow'
    // | 'keyboardDidShow'
    // | 'keyboardWillHide'
    // | 'keyboardDidHide'
    // | 'keyboardWillChangeFrame'
    // | 'keyboardDidChangeFrame';
    console.log('注册键盘事件');
    const willShow = Keyboard.addListener('keyboardWillShow', () => {
      console.log('keyboardWillShow');
    })
    const didShow = Keyboard.addListener('keyboardDidShow', () => {
      console.log('keyboardDidShow');
    })
    const willHide = Keyboard.addListener('keyboardWillHide', () => {
      console.log('keyboardWillHide');
    })
    const didHide  = Keyboard.addListener('keyboardDidHide', () => {
      console.log('keyboardDidHide');
    })
    const willChange = Keyboard.addListener('keyboardWillChangeFrame', () => {
      console.log('keyboardWillChangeFrame');
    })
    const didChange = Keyboard.addListener('keyboardDidChangeFrame', () => {
      console.log('keyboardDidChangeFrame');
    })
    return () => {
      willShow.remove();
      didShow.remove();
      willHide.remove();
      didHide.remove();
      willChange.remove();
      didChange.remove();
  }, []);    
  return (
    // <KeyboardAwareScrollView
    //   style={styles.scroll}
    //   resetScrollToCoords={resetScrollToCoords}
    //   enableAutomaticScroll={enableAutomaticScroll}
    //   extraHeight={extraHeight}
    //   extraScrollHeight={extraScrollHeight}
    //   enableResetScrollToCoords={enableResetScrollToCoords}>
    <ScrollView
      style={styles.scroll}
      >
      <Button title="关闭键盘"  onPress={() => {
        Keyboard.dismiss();
      }}/>
      {Labels.map(item => {
        return (
          <View key={item}>
            <Text>{item}:</Text>
            <TextInput style={styles.input} placeholder="请输入" />
          </View>
        );
      })}
    </ScrollView> 
    // </KeyboardAwareScrollView> 
  );
};
const styles = StyleSheet.create({
  scroll: {
    backgroundColor: '#f0f0f0',
  },
  input: {
    width: 300,
    marginLeft: 20,
    borderWidth: 2,
    borderColor: 'black',
    height: 40,
    borderRadius: 10,
    fontSize: 26,
    paddingLeft: 6,
    marginTop: 10,
    marginBottom: 10,
  },
});

有问题可在微信公众号或者掘金社区私信留言

更多内容可关注\

我的公众号悬空八只脚