【问题标题】:Button can't be clicked while keyboard is visible - react native键盘可见时无法单击按钮-本机反应
【发布时间】:2020-12-30 20:56:18
【问题描述】:

我有一个 textInput 和一个按钮。当 textInput 聚焦时,键盘会出现,但如果您在键盘可见的情况下点击按钮,首先键盘会消失,然后只有您可以点击按钮。为什么?我怎样才能使它工作,以便可以在键盘可见的情况下点击按钮?在android studio(android native)中开发的应用程序可以使键盘可见的点击监听器。但是在本机反应中,它不起作用。如果你点击按钮以外的任何地方,那么键盘应该会消失,不是吗?但是,如果您在键盘可见的情况下点击按钮,btn 应该会接收到侦听器。我正在安卓设备上测试它。

P.S 你可以在这里试试:https://snack.expo.io/@codebyte99/addcomponents

<TextInput
    placeholder={'Type smth'}
    style={[{ borderBottomColor: 'gray', borderBottomWidth: 1 }]}
/>

<TouchableOpacity onPress={() => { this._onPressOut(); }}>
  <Text>click here</Text>
</TouchableOpacity>

【问题讨论】:

    标签: react-native react-native-android


    【解决方案1】:

    滚动视图包含一个道具keyboardShouldPersistTaps,它处理滚动视图内的键盘点击行为。

    对于您的情况,请将其命名为&lt;ScrollView keyboardShouldPersistTaps='handled'&gt;

    这里是博览会链接scrollview with keyboard

    【讨论】:

    • This comment on GitHub 声明应在所有父级ScrollViews 上设置keyboardShouldPersistTaps='handled'the fix for me was that I had to set keyboardShouldPersistTaps on ALL scrollview ancestors of my Modal... otherwise, if any ScrollView in your parent tree do the default keyboardShouldPersistTaps="none", the keyboard will be dismissed (in my case I want to prevent a dismiss).
    【解决方案2】:

    作为@thakur-karthik 答案的补充:

    值得注意的是,在滚动视图场景中,当您在 react-native 模态中使用滚动视图时会发生一些奇怪的情况。

    仅仅在模态的滚动视图上添加keyboardShouldPersistTaps={'always'}单独是行不通的。

    如果您在任何祖先中有滚动视图,它们必须在其组件上声明keyboardShouldPersistTaps={'always'}

    【讨论】:

      【解决方案3】:

      仅供参考:如果您使用的是react-native-keyboard-aware-scroll-view,您需要这样做:

      <KeyboardAwareScrollView keyboardShouldPersistTaps="always">
      

      【讨论】:

        猜你喜欢
        • 2014-06-13
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 2017-10-15
        • 2023-03-24
        • 2020-04-22
        • 2015-02-23
        • 1970-01-01
        相关资源
        最近更新 更多