本文介绍了键盘在屏幕上时不会修改 useWindowDimensions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试计算键盘出现时屏幕的高度(在 Android 设备上进行测试).尝试使用 useWindowDimensions 但宽度/高度不会改变.我在这里创建了一个小例子.
I'm trying to calculate the height of the screen when the keyboard appears (testing on an Android device). Tried with useWindowDimensions but width/height doesn't change. I've created a small example here.
export default function App() {
const [something, setSomething] = useState("")
const window = useWindowDimensions();
return (
<View style={styles.container}>
<Text>Here I'm expecting width or height to change if I focus
on the input because keyboard will be shown</Text>
<Text style={styles.paragraph}>
width: {window.width} - height: {window.height}
</Text>
<TextInput
style={[styles.textInput]}
autoCompleteType="email"
placeholder="something"
value={something}
onChangeText={(text) => setSomething(text)}
autoCapitalize="none"
autoCorrect={false}
/>
</View>
);
}
还有其他方法吗?
推荐答案
我不确定重新定义 useDimensions
是否可行.
I'm not sure if re-defining the useDimensions
would work.
无论如何,您可以使用 react-native- 中的 Keyboard Hooks社区获取键盘的高度,然后减去初始高度.
Anyway, you could use the Keyboard Hooks from react-native-community to get the height of the keyboard and then subtract to the initial height.
它可能也适用于 KeyboardListener
It probably works well with a KeyboardListener as well
这篇关于键盘在屏幕上时不会修改 useWindowDimensions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!