如何解决按下按钮只会关闭键盘
我有一个Modal
,其中包含一个TextInput
和一个Button
。打开模式对话框后,将出现键盘,该键盘使您可以在TextInput中输入文本。
我的问题是,当我第一次按下按钮时,键盘被关闭了,但是按钮onPress
的方法没有被触发。
import React from 'react';
import {
StyleSheet,View,Modal,Text,TextInput,InteractionManager,ScrollView,TouchableWithoutFeedback,TouchableOpacity,Keyboard,} from 'react-native';
import theme from '../../styles/theme.style';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import I18n from '../../utils/i18n';
class RenameDrillModal extends React.Component {
// Enables to open the keyboard when the component is rendered
inputRef = React.createRef();
componentDidMount() {
this.focusInputWithKeyboard();
}
focusInputWithKeyboard() {
InteractionManager.runAfterInteractions(() => {
this.inputRef.current.focus();
});
}
render() {
return (
<View>
<Modal
animationType="fade"
transparent
visible
onRequestClose={() => {
this.props.close();
}}
>
{/* The sole purpose of the three following tags (TouchableOpacity,TouchableWithoutFeedback) is to close the modal when clicking outside of it */}
<TouchableOpacity
activeOpacity={1}
style={{ flex: 1,justifyContent: 'center',alignItems: 'center' }}
onPressOut={() => {
this.props.close();
}}
>
<ScrollView directionalLockEnabled>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.modalTitleView}>
<Text style={styles.modalTitleText}>{`${this.props.drillTitle}`}</Text>
<View style={{ minWidth: '50%',flexDirection: 'row' }}>
<TextInput
style={{ minWidth: '90%',margin: 5,marginBottom: 0,flex: 1 }}
placeholder={I18n.t('drillEditor.drillManager.clickHeretoRename')}
onChangeText={text => this.props.textModified(text)}
ref={this.inputRef}
/>
<MaterialCommunityIcons
name="check"
color={theme.COLOR_PRIMARY}
size={26}
onPress={() => {
console.log('Rename,onPress called');
this.props.close();
this.props.confirmNewName();
}}
/>
</View>
</View>
</TouchableWithoutFeedback>
</ScrollView>
</TouchableOpacity>
</Modal>
</View>
);
}
}
const styles = StyleSheet.create({
modalTitleView: {
margin: 20,backgroundColor: 'white',borderRadius: 20,padding: 20,alignItems: 'center',shadowColor: '#000',shadowOffset: {
width: 0,height: 2,},shadowOpacity: 0.25,shadowRadius: 3.84,elevation: 5,modalTitleText: {
fontWeight: 'bold',fontSize: 20,marginBottom: 15,textAlign: 'center',item: {
backgroundColor: theme.BACKGROUND_COLOR_LIGHT,padding: 8,borderRadius: 10,marginVertical: 8,marginHorizontal: 8,flexDirection: 'row',justifyContent: 'space-between',});
export default RenameDrillModal;
第二次按下该按钮即可,但是您知道我既可以关闭键盘又可以只按一次按钮来按下按钮吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。