[英]React Native - Text Input - Long text input, cursor doesn't go at the beginning after clicking the return key type
I have this simple Text Input but the problem is that on Android Devices, when the user has written a long text, the cursor doesn't go automatically in the beginning of the field but it stays in the end.我有这个简单的文本输入,但问题是在 Android 设备上,当用户写了长文本时,cursor 不会 go 自动留在字段的开头。 How can I fix this?我怎样才能解决这个问题?
<TextInput
key={currencyTypeId}
ref={forwardRef}
style={styles.input}
onChangeText={onChangeText}
value={inputValue}
editable={editable}
autoCorrect={false}
autoCompleteType='off'
returnKeyType={returnKeyType}
placeholder={placeholder}
placeholderTextColor={placeholderColor}
keyboardType={keyboardType}
/>
As it turns out there was no solution for this for andorid devices.事实证明,对于 andorid 设备没有解决方案。 I managed to find another solution by using this property onContentSizeChange and defining a state for input height so the text could break in more than 1 line.我设法通过使用此属性onContentSizeChange并为输入高度定义 state 找到了另一个解决方案,因此文本可能会中断超过 1 行。
const [height, setHeight] = useState();
Add this to TextInput component将此添加到 TextInput 组件
onContentSizeChange={({
nativeEvent: {
contentSize: { height: newHeight },
},
}) => {
setHeight(newHeight);
}}
If you have a problem with the height style in the text input styles, pass the height as condition to the styles and define the height styles you want based on it.如果您对文本输入 styles 中的高度样式有问题,请将高度作为条件传递给 styles 并根据它定义您想要的高度 styles。
Example:例子:
style={[
styles.input,
inputValue ? (inputValue.length > 30 ? { height: heightInput } : { height: '10%'}) : null,
]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.