React Native:如何在方法中触发TextInput实例模糊?

时间:2017-05-24 15:48:32

标签: javascript react-native

我单击导航器上的按钮,当前屏幕的一个TextInput是焦点。 我需要设置TextInput的实例模糊以触发textChange函数执行某些操作。

就我而言,TextInput的光标一直闪烁。

refs.username.blur()不起作用。

enter image description here

我的代码:

class UploadRecordII extends Component{
  static navigationOptions = ({ navigation, screenProps }) => ({
    headerRight: (<Button containerStyle={{marginRight: 12,}} onPress={ UploadRecordII.onSubmit}>
                  <Text style={{color: 'white',fontSize: 18,}}>next</Text>
                </Button>),
  });
.....


   static onSubmit() {
    let that = UploadRecordII.instance;
     debugger;
     that.refs.username.blur();
     debugger;
    that.props.commitCreateRecordUpdate(that.refs.username._lastNativeText,that.imageList)
    that.props.navigation.navigate('Record3');
  }



......  in render(){
....

<TextInput ref = 'username' placeholder='' editable = {true} maxLength = {500}
                  keyboardType='default' multiline={true}
                  style={styles.input} underlineColorAndroid='rgba(0,0,0,0)'
                           onBlur={  ()=> this.textChange()}
                  clearButtonMode='always' placeholderTextColor='#D6D0D8'></TextInput>



...
}

1 个答案:

答案 0 :(得分:0)

refs不再像预期的那样以字符串形式工作。你需要定义你的例如

class UploadRecordII extends Component {
  constructor(props) {
    super(props);

    this.usernameInput = null;
  }

  onSubmit() {
    this.usernameInput.blur();
  }

  render() {
    return (
    <TextInput ref={(input) =>  this.usernameInput = input} ... />
    )
  }
}