我当前要在父组件或模态可见时自动打开键盘,这是我想要的,但是问题是,当我先关闭模态时,键盘将被关闭,然后模态将被关闭。被解雇,这会导致不良的UI交互性。我想保持键盘静止不动,只关闭父组件或模态,而不是两者。当KeyboardShouldPersistTapsis设置为“ always”时,我在KeyboardAvoidingView中使用了scrollView。您可以在下面找到我的代码:
const amountInput = () => (
<View style={styles.inputAndTextContainer}>
<Text style={styles.textStyle}>ETB</Text>
<TextInput
onKeyPress={({ nativeEvent }) => {
return nativeEvent.key === 'Backspace' ? clearLastChar() : null;
}}
caretHidden
ref={textInputRef}
style={styles.inputStyle}
autoCapitalize="none"
autoCorrect={false}
keyboardType="numeric"
defaultValue={amount.value}
/* value={amount.value} */
autoFocus
maxLength={6}
onChangeText={num => {
setAmount({ value: addCommaToNumber(num) });
}}
/>
</View>
);
return (
<View>
<Modal
isVisible={isVisible}
animationIn="bounceInUp"
animationOut="slideOutDown"
animationOutTiming={200}
animationInTiming={200}
style={{ margin: 0 }}
>
<KeyboardAvoidingView behavior="padding">
<Header />
<ScrollView
keyboardShouldPersistTaps="always"
alwaysBounceVertical={false}
>
{amountInput()}
{state && state.length ? someFunc() : anotherFunc()}
</ScrollView>
{renderModal()}
{state ? someFunc() : someotherfunc()}
</KeyboardAvoidingView>
</Modal>
</View>
)