React Native中的隐藏TextInput

时间:2019-10-30 07:43:44

标签: react-native

我正在一个项目中,其中我已连接到扫描仪。扫描仪在扫描后将发送文本。现在,我有一个“文本输入(隐藏)”字段以从扫描仪获取详细信息,但是当文本输入获得焦点时,我面临的问题是键盘正在显示。我尝试使用Keyboard.dismiss(),但这也从TextInput移除了焦点(现在从Scanner返回的文本不再被TextInput监听)。我该如何解决这个问题?

以下是代码

 <TextInput
            style={Style.hiddenInput}
            autoFocus={true}
            multiline
            onFocus={Keyboard.dismiss}
            onChangeText={this._onHiddenTextChangeText}
            value={this.state.hiddenInput}
          />

样式

 hiddenInput: {
    width: 0,
    height: 0,
  },

2 个答案:

答案 0 :(得分:0)

我能想到的最佳方法是将扫描的值复制到Clipboard(可能使用“扫描仪设置”),并连续检查Clipboard中的数据。

componentDidMount() {
  this.getReadyToScan();
}

getReadyToScan() {
  try {
    let content = await Clipboard.getString();

    // Do whatever you want

    Clipboard.setString('');
  } catch (e) {
    Clipboard.setString('');
  } finally {
    // Adding a little bit of sleep
    setTimeout(() => this.getReadyToScan(), 200);
  }
}

有人要求将keyboard='none'添加到React-Native的TextInput中,但是他们还没有解决。

答案 1 :(得分:0)

我通过添加本机包解决了此问题。我已在以下github链接中添加了示例

https://github.com/mohanprasathsj/reactnative-hidekeyboardonfocusexample