在屏幕上永久安装键盘

时间:2019-11-12 14:30:16

标签: javascript reactjs react-native

是否可以使键盘始终在屏幕上可见?

屏幕上有:

  • 一个TextInput(多行)
  • 两个FlatList

当我在TextInput中键入内容时可以,但是当我将内容更改为FlatList时,键盘被隐藏了

无论我在屏幕上做什么,我都希望键盘始终可见

我测试了什么

  • keyboardShouldPersistTaps='always'无法正常工作
  • 在一开始,我将autoFocus设置为TextInput,并且在隐藏键盘时进行了每次更改,请再次显示它。我以为可以做到这一点,以使过渡不可见,但无法正常运行
componentDidUpdate() {
    setTimeout(() => {
      this.nameInput.focus();
    }, 1);
  }

有什么办法吗?

1 个答案:

答案 0 :(得分:0)

您应在keyboardWillHide上添加事件监听器,并在触发事件时集中注意nameInput TextField

类似这样的东西:

import React, { Component } from "react";
import { Keyboard, TextInput } from "react-native";

class MyComponent extends Component {
  componentDidMount() {
    this.keyboardWillHideListener = Keyboard.addListener(
      "keyboardWillHide",
      this._keyboardWillHide,
    );
  }

  componentWillUnmount() {
    this.keyboardWillHideListener.remove();
  }

  _keyboardWillHide() {
    // Focus the input
  }

  render() {
    // Your UI here
  }
}

更多信息可以在这里找到:https://facebook.github.io/react-native/docs/keyboard.html