android - 未定义不是 TextInput onSubmitEditing Focus 的对象

标签 android ios react-native input

当用户按下键盘上的下一个按钮时,我尝试聚焦下一个字段。但我一直收到undefined is not an object (_this2.passwordInput.focus)。

我指的是this实现它的问题。但它没有用。

我有 CustomInputText,这就是我实现它的方式。

<CustomEditText
    labelText="Email or Phone Number"
    returnKeyType = "next"
    onSubmitEditing={ () => this.passwordInput.focus() }
    onChangeText={text => this.setState({ username: text })}
    blurOnSubmit={false}
    />

<CustomEditText
    ref={(ref) => { this.passwordInput = ref; }}
    labelText="Password"
    secureText={true}
    onChangeText={text => this.setState({ password: text })}
    />

CustomEditText.js

const CustomEditText = props => {
  return (
    <View style={styles.container}>
      <SmallText fontFamily="semi-bold" color={props.darkText? Colors.TEXT_PRIMARY_COLOR : "#fff"}>
        {props.labelText}
      </SmallText>

      <TextInput
        ref = {props.ref}
        blurOnSubmit= {props.blurOnSubmit}
        onSubmitEditing = {props.onSubmitEditing}
        returnKeyType = {props.returnKeyType}
        onFocus = {props.onFocus}
        autoCapitalize="none"
        editable={props.editable}
        onChangeText={props.onChangeText}
        keyboardType={props.keyboardType}
        {...props}
        value={props.value}
      />
      {props.secondaryLabelText ? (
        <ExtraSmallText
          fontFamily="semi-bold"
          color={props.darkText? Colors.TEXT_PRIMARY_COLOR : "#fff"}
          style={styles.secondaryLabelStyle}
        >
          {props.secondaryLabelText}
        </ExtraSmallText>
      ) : null}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    margin: 5
  },
  textInputStyle: {
    backgroundColor: "#fff",
    height: 45,
    borderRadius: 5,
    borderWidth: 1,
    padding: 5,
    marginTop: 5,
    color:"#000"
  },
  secondaryLabelStyle: {
    marginTop: 5
  }
});

CustomEditText.propTypes = {
  labelText: propTypes.string.isRequired,
  secondaryLabelText: propTypes.string,
  keyboardType: propTypes.string,
  secureText: propTypes.bool,
  placeholderText: propTypes.string,
  placeholderTextColor: propTypes.bool,
  onChangeText: propTypes.func,
  darkText: propTypes.bool,
  editable: propTypes.bool,
  onFocus: propTypes.func,
  pointerEvents: propTypes.string,
  returnKeyType : propTypes.string,
  onSubmitEditing: propTypes.func,
  ref: propTypes.func,
  blurOnSubmit: propTypes.bool

};

export default CustomEditText;

最佳答案

使用以下代码更新您的 CustomTextInput.js 文件

<TextInput
        ref={input => props.inputRef && props.inputRef(input)}
        blurOnSubmit= {props.blurOnSubmit}
        onSubmitEditing = {props.onSubmitEditing}
        returnKeyType = {props.returnKeyType}
        onFocus = {props.onFocus}
        autoCapitalize="none"
        editable={props.editable}
        onChangeText={props.onChangeText}
        keyboardType={props.keyboardType}
        {...props}
        value={props.value}
      />

然后像这样使用它

<CustomEditText
          inputRef={(ref) => this.passwordInput = ref}
          labelText="Password"
          secureText={true}
          // onChangeText={text => this.setState({ password: text })}
          />

关于android - 未定义不是 TextInput onSubmitEditing Focus 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57671879/

相关文章:

ios - 当我在 Xcode 中设置 "Horizontal centre in container"时,为什么框架会跳到 View 的顶部?

react-native - React Native Platform OS 突然不工作

react-native - 当用Expo制作的应用程序打开时,它不会进入休眠模式,它始终保持打开状态

android - 如何在 LinearLayout 上绘制(手指绘画)后将其保存为图像

Android 应用程序在第二次运行时崩溃

android - 使用 RxJava 从 Internet 获取数据时,ListView 无法显示项目数据

ios - 有没有一种方法可以一次绑定(bind) Assets 而不是每个命令编码器?

Java将字节数组转换为UTF-8字符串

IOS:IBAction 有两个 Action

ios - 解压速度太慢