我有一个简短的表单,它有两个FormInput字段(来自react-native-elements)。我尝试使用几种方法在触摸文本输入之外的屏幕上的任何位置时移开焦点(并隐藏键盘)。
<View style={{flex:1}}>
<ScrollView keyboardShouldPersistTaps="handled">
<FormInput
placeholder='Phone Number'
errorStyle={{ color: 'red' }}
inputStyle={{ color: 'white', fontSize: 20, fontFamily: 'Roboto-Thin', borderBottomColor: 'transparent'}}
placeholderTextColor='white'
onChangeText = {this.onPhoneChange.bind(this)}
containerStyle = {{ borderBottomWidth: 1, borderBottomColor: '#4DB1AD', width: '90%'}}
keyboardType='numeric'
maxLength={10}
/>
</ScrollView>
</View>
我也尝试过
<TouchableWithoutFeedback style={{flex: 1}} onPress={Keyboard.dismiss} accessible={false}>
<View>
<FormInput
placeholder='Phone Number'
errorStyle={{ color: 'red' }}
inputStyle={{ color: 'white', fontSize: 20, fontFamily: 'Roboto-Thin', borderBottomColor: 'transparent'}}
placeholderTextColor='white'
onChangeText = {this.onPhoneChange.bind(this)}
containerStyle = {{ borderBottomWidth: 1, borderBottomColor: '#4DB1AD', width: '90%'}}
keyboardType='numeric'
maxLength={10}
onSubmitEditing={Keyboard.dismiss}
onBlur={Keyboard.dismiss}
blurOnSubmit={true}
/>
</View>
</TouchableWithoutFeedback>
和此方法
<TouchableOpacity activeOpacity={1} onPress={() => Keyboard.dismiss()}>
<FormInput
placeholder='Phone Number'
errorStyle={{ color: 'red' }}
inputStyle={{ color: 'white', fontSize: 20, fontFamily: 'Roboto-Thin', borderBottomColor: 'transparent'}}
placeholderTextColor='white'
onChangeText = {this.onPhoneChange.bind(this)}
containerStyle = {{ borderBottomWidth: 1, borderBottomColor: '#4DB1AD', width: '90%'}}
keyboardType='numeric'
maxLength={10}
/>
</TouchableOpacity>
不幸的是,这些解决方案都不适合我。