【问题标题】:ReactNative TextInput FocusReactNative TextInput 焦点
【发布时间】:2017-12-18 08:54:29
【问题描述】:

我的应用程序中有一个表单,我希望用户能够通过单击“下一步”返回按钮转到下一个 TextInput。 我的输入组件:

    export default class Input extends Component {

  focusNextField = (nextField) => {
    console.log('NEXT FIELD:', nextField);
    this.refs[nextField].focus();
  }

  render() {

    var keyboardType = this.props.keyboardType || 'default';
    var style = [styles.textInput, this.props.style];

    if (this.props.hasError) style.push(styles.error);

    return (
      <View style={styles.textInputContainer}>
        <TextInput
          placeholder={this.props.placeholder}
          onChangeText={this.props.onChangeText}
          style={style}
          blurOnSubmit={false}
          ref={this.props.reference}
          returnKeyType= {this.props.returnType}
          onSubmitEditing={() => this.focusNextField(this.props.fieldRef)}
          secureTextEntry={this.props.isPassword}
          value={this.props.value}
          keyboardType={keyboardType}
          underlineColorAndroid="transparent" />
        {this.props.hasError && this.props.errorMessage ? <Text style={{ color: 'red' }}>{this.props.errorMessage}</Text> : null}
      </View>
    );
  }
}

以及如何使用:

<Input onChangeText={(email) => this.setState({ email })} value={this.state.email} returnType={"next"} reference={'1'} fieldRef={'2'} keyboardType="email-address" />

      <Text style={{ color: '#fff', marginTop: 10, }}>Password</Text>
      <Input onChangeText={(password) => this.setState({ password })} value={this.state.password} returnType={"done"}
       reference={'2'} fieldRef={'2'} isPassword={true} />

但我得到了错误:

undefined is not an object (evaluating _this.refs[nextField].focus)

【问题讨论】:

    标签: react-native


    【解决方案1】:

    不确定您是否仍在尝试这样做,但对于遇到问题的其他人,请执行以下操作:

    1. 将此代码添加到您的导入中(导入中的任何位置)

      import { findNodeHandle } from 'react-native';
      import TextInputState from 'react-native/lib/TextInputState';
      export function focusTextInput(node) {
        try {
          TextInputState.focusTextInput(findNodeHandle(node));
        } catch(e) {
          console.log("Couldn't focus text input: ", e.message)
        }
      };
      
    2. 将以下行添加到您的构造函数中

      this.focusNextField = this.focusNextField.bind(this);
      this.inputs = {};
      
    3. 将以下函数添加到您的类中

      focusNextField(id) {
        this.inputs[id].focus();
      }
      
    4. 编辑您的TextInput 如下:

      <TextInput
        onSubmitEditing={() => {this.focusNextField('two');}}
        ref={ input => {this.inputs['one'] = input;}}
      />
      <TextInput
        onSubmitEditing={() => {this.focusNextField('three');}}
        ref={ input => {this.inputs['two'] = input;}}
      />
      ....
      

    Here is the source of that answer

    为我工作了 0.45。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      相关资源
      最近更新 更多