我希望在按下搜索按钮解除键盘时从TextInput
获取当前文本。为了获取文本,我使用onSubmitEditing
但只有在我将returnKeyType设置为' search'时才会调用它。这是一段代码片段。
render() {
return (
<View style={{flex:1, justifyContent:'center'}}>
<TextInput
style={{alignSelf:'stretch'}}
onChangeText={(searchInput) => this.setState({ searchInput }) }
placeholder='Search'
returnKeyType='search'
onFocus={() => console.log('onFocus')}
onBlur={() => console.log('onBlur')}
onChange={(event) => console.log('onChange text: ' + event.nativeEvent.text)}
onEndEditing={(event) => console.log('onEndEditing text: ' + event.nativeEvent.text)}
onSubmitEditing={(event) => console.log('onSubmitEditing text: ' + event.nativeEvent.text)}
/>
</View>
);
}
只在android中发生(onSubmitEditing
在iOS中调用一次)。当我使用RN 0.24时,onSubmitEditing
也被调用一次,但在最新版本(0.30)中被调用两次。这是预期的行为吗?有没有办法在按下搜索按钮解除键盘时取一次文本?
答案 0 :(得分:1)
使用textinput.blur
解决此问题<TextInput
ref={component => this._textInput = component}
onSubmitEditing={this._onSubmitEditing.bind(this)}
onEndEditing={this._onPressSearch.bind(this)}
returnKeyType='search'
style={styles.inputText}/>
_onSubmitEditing(){
this._textInput.blur();
}
_onPressSearch(event){
console.log(event.nativeEvent.text);
}