TextInput 的退格和长度 = 0 时如何获取事件

时间:2021-07-26 02:46:45

标签: react-native react-native-textinput

这是我的 React Native 代码: 从“反应”导入反应; import { View, StyleSheet, TextInput } from "react-native";

const UselessTextInput = () => {
  const [text, onChangeText] = React.useState("");
  const [number, onChangeNumber] = React.useState(null);

  return (
    <View>
      <TextInput
        style={styles.input}
        onChangeText={onChangeText}
        value={text}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
  },
});

export default UselessTextInput;

链接展示:https://snack.expo.dev/@robocon321/textinput

1 个答案:

答案 0 :(得分:1)

使用onKeyPress

onKeyPress={({ nativeEvent }) => {
  if (nativeEvent.key === 'Backspace') {
     alert("Backspace is pressed")
  } 
}}

尝试小吃here

在 ios 和 android 上工作

安卓注意!

<块引用>

注意:在 Android 上只处理来自软键盘的输入,而不处理硬件键盘输入。

soft keyboard屏幕键盘或软件键盘)就像手机或平板电脑屏幕上的键盘一样。
hardware keyboard外部键盘)类似于带 USB 的键盘(Android 支持),但您可以忽略它。