【发布时间】:2019-10-28 11:34:25
【问题描述】:
我有一个非常令人沮丧的情况。试图让键盘消失并检测子行中的 onPress 事件处理程序。
我的代码如下所示:
_renderRow = (prediction) => {
return (
<TouchableOpacity onPress={() => {
Keyboard.dismiss();
this.setState({ location: prediction.description });
}}>
<View style={styles.listItemContainer}>
<Text>{prediction.description}</Text>
</View>
</TouchableOpacity>
)
}
render() {
return (
<View style={styles.wrapper}>
{/* style={[this.state.predictions.length > 0 ? styles.searchContainerSuggest : styles.searchContainer]} */}
<View style={styles.searchContainerSuggest}>
<View style={{paddingLeft: 10, height: 45, display: 'flex', justifyContent: 'center'}}>
<TextInput
placeholder="Enter location"
value={this.state.location}
onChangeText={location => this.onChangeLocation(location)}
style={styles.textInput}
/>
</View>
{this.state.predictions.length && this.state.location !== '' ?
<FlatList
keyboardShouldPersistTaps={'handled'}
refreshing={!this.state.loaded}
initialNumToRender={10}
enableEmptySections={true}
data={this.state.predictions}
keyExtractor={(_, index) => index.toString()}
renderItem={ ({item: prediction}) => this._renderRow(prediction) } />
: null}
</View>
</View>
);
}
关于如何调试此问题,我可能需要一两个帮助。
查找了几个有关如何处理隐藏键盘并允许同时按下特定选择的示例。
我认为keyboardShouldPersistTaps 将允许选择子选项。选择后,onPress 事件处理程序将触发,这将是我调用 Keyboard.dismiss() 以隐藏键盘的地方。好像没用。
【问题讨论】:
标签: javascript reactjs react-native react-native-flatlist