代码之家  ›  专栏  ›  技术社区  ›  Ariel

当键盘在屏幕上时,不修改useWindowDimensions

  •  0
  • Ariel  · 技术社区  · 5 年前

    我试图计算键盘出现时屏幕的高度(在Android设备上测试)。尝试使用useWindowDimensions,但宽度/高度不变。我创造了一个小例子 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>
      );
    }
    

    还有别的办法吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   João Rocha    5 年前

    我不确定是否要重新定义 useDimensions 会有用的。

    不管怎样,你可以用 Keyboard Hooks 从react native community得到键盘的高度,然后减去初始高度。

    它可能也适用于键盘监听器

        2
  •  0
  •   Abrah_dago_313    5 年前

    检查AndroidManifest。xml

    看看: <活动

    android:name=“.MainActivity”

    android:label=“@string/app_name”

    看看这个 =>android:configChanges=“键盘|键盘隐藏|方向|屏幕大小| uiMode”

    如果错过了这一行 加上它。

    推荐文章