reactjs - 当我运行 React Native Expo 应用程序时,键盘覆盖 TextInput 表单
问题描述
当我使用 expo 运行应用程序并使用我的 DateFormInput 组件时,键盘覆盖了 TextInput。我尝试使用 I 、“@pietile-native-kit/keyboard-aware-scrollview”、“@types/react-native-keyboard-spacer”、“react-native-keyboard-aware-scroll-view”和“react -native-keyboard-spacer”,但它们都不能在 IOS 或 Android 上运行。
这是我渲染 TextInput (DateFormInput) 的组件:
import React from 'react';
import {
Text,
StyleSheet,
View,
KeyboardAvoidingView,
ScrollView,
} from 'react-native';
import Box from './Box';
import Button from './Button';
import DateFormInput from './DateFormInput';
import { isValidDate } from '../util';
import strings from '../strings';
interface Props {
handleSubmit: (formName: string) => void;
handleOnChange: (formName: string, input: string) => void;
handleCancelButton: (formName: string) => void;
toggleInputError: (formName: string, clear?: boolean) => void;
inputFormText: string;
inputError: boolean;
formName: string;
}
export const DDCAndDTNInputForm: React.FC<Props> = (props: Props) => {
return (
<Box style={styles.box}>
<Text>
{props.formName === 'DrugTest' ? strings.DrugTestDate : strings.DDCDate}
</Text>
<View style={{ flex: 1 }}>
<KeyboardAvoidingView behavior="padding" style={styles.box}>
<ScrollView style={{ flex: 1 }}>
<DateFormInput
type={'datetime'}
options={{ format: 'MM/DD/YYYY' }}
placeholder={'MM/DD/YYYY'}
value={props.inputFormText}
error={props.inputError}
onChangeText={text => props.handleOnChange(props.formName, text)}
onBlur={e => {
isValidDate(props.inputFormText)
? props.handleSubmit(props.formName)
: props.toggleInputError(props.formName, true);
}}
/>
</ScrollView>
</KeyboardAvoidingView>
</View>
<Text style={{ color: 'red' }}>
{props.inputError ? 'Type a valid date' : null}
</Text>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-around',
}}
>
<Button
viewStyle={styles.linkButton}
title={'CANCEL'}
onPress={() => {
props.handleCancelButton(props.formName);
}}
/>
<Button
viewStyle={styles.linkButton}
title={'SAVE DATE'}
onPress={() => {
isValidDate(props.inputFormText)
? props.handleSubmit(props.formName)
: props.toggleInputError(props.formName, true);
}}
/>
</View>
</Box>
);
};
export default DDCAndDTNInputForm;
const styles = StyleSheet.create({
linkButton: {
paddingVertical: 8,
paddingHorizontal: 16,
marginVertical: 8,
flex: 1,
},
box: {
padding: 24,
marginBottom: 16,
flex: 1,
},
});
这是我的自定义 InputText(DateFormInput):
import React from 'react';
import {
Text,
StyleProp,
TextStyle,
StyleSheet,
KeyboardType,
View,
NativeSyntheticEvent,
TextInputFocusEventData,
} from 'react-native';
import { TextInputMask } from 'react-native-masked-text';
import { textStyle, colors } from '../styles';
const styles = StyleSheet.create({
input: {
marginVertical: 8,
padding: 16,
borderWidth: 2,
borderColor: colors.sectionBackground,
borderRadius: 8,
backgroundColor: colors.white,
},
});
export default (props: {
label: string;
value: string;
labelStyle: StyleProp<TextStyle>;
keyboardType?: KeyboardType;
onChangeText: (text: string) => void;
onBlur?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
error?: boolean;
defaultValue?: string;
}) => (
<View style={{ flexDirection: 'column', alignContent: 'stretch' }}>
<Text style={[textStyle.formLabel, props.labelStyle]}>{props.label}</Text>
<TextInputMask
type={'datetime'}
options={{ format: 'MM/DD/YYYY' }}
placeholder={'MM/DD/YYYY'}
style={[styles.input, props.error ? { borderColor: colors.red } : null]}
value={props.value}
onChangeText={props.onChangeText}
onBlur={props.onBlur}
/>
</View>
);
解决方案
对于在 sdk 42 上仍然面临同样问题的任何人,我发现由于某些未知原因,键盘覆盖了我的TextInput
,因为它们被包裹在View
. 因此,将我包裹起来TextInput
, ScrollView
允许键盘在推出时向上推动输入。
推荐阅读
- functional-programming - 伊莎贝尔结构证明
- vb.net - 如何基于另一个按钮引用一个按钮
- css - Scrapy / xpath 不起作用 - 仅适用于 css
- python - Python 在不同环境中比较 int(Docker 与本地机器)
- google-cloud-platform - 资源不足 - Google Cloud
- javascript - 使用 DOM 操作无法捕捉虚拟键盘的特殊键
- linux - BBB CAN TX 仅在环路情况下工作
- jquery - 如何在文档准备好时自动按下打印屏幕按钮?实际上没有任何用户按下按钮
- javascript - HTML,JavaScript。如何在同一个弹窗按钮上显示不同的信息
- android - 无法在 KVM 机器中启动 Android AVD