如何在React Native中禁用键盘

时间:2018-07-02 11:35:51

标签: react-native

您好,我创建了屏幕键盘组件,并且想禁用平台键盘,该如何进行检查?

<TextInput
        secureTextEntry
        ref="Pin"
        selectionColor="#656565"
        keyboardType="numeric"
        //   compareWithCode=""
        activeColor="#656565"
        inactiveColor="#fff"
        autoFocus={false}
        ignoreCase
        codeLength={4}
        inputPosition="center"
        size={50}
        onFulfill={isValid => this}
        codeInputStyle={{ borderWidth: 1.5 }}
      />

7 个答案:

答案 0 :(得分:1)

我也有问题。没有其他解决方案对我有用。这将显示文本输入字段,它将是可单击但不可编辑的。

<TouchableOpacity onPress={this.openPinKeyboard}>
   <View pointerEvents="none">
      <Input editable={false} value="1234" />
   </View>
</TouchableOpacity>

答案 1 :(得分:1)

像这样在showSoftInputOnFocus={false}中写<TextInput>

<TextInput showSoftInputOnFocus={false} />

答案 2 :(得分:1)

试试这个解决方案,我希望这对 android 和 ios 都适用...

// Step 1: Get Keyboard, TouchableWithoutFeedback from ‘react-native’;
        import { View, TextInput, StyleSheet, Keyboard,  TouchableWithoutFeedback } from 'react-native';

        // Step 2: Create an arrow function to write dismiss keyboard code
        const DismissKeyboard = ({ children }) => (
            <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
                {children}
            </TouchableWithoutFeedback>
            );

        // Step 3: Wrap all TextInput inside <DismissKeyboard> </DismissKeyboard>
        //Example
        <DismissKeyboard>
            <View style={styles.container}>
                <TextInput style={styles.input} placeholder="email" />
                <TextInput style={styles.input} placeholder="password" />
            </View>
        </DismissKeyboard>

答案 3 :(得分:0)

如果不起作用,您可以尝试将keyboardType设置为none。另一种选择是将editable道具设置为false

可以在这里找到可能的答案:https://github.com/facebook/react-native/issues/14045

答案 4 :(得分:0)

import sys from PyQt5 import QtWidgets from PyQt5.QtWidgets import QComboBox, QGridLayout, QWidget, QPushButton, QListWidget lines2 = [] lines3 = [] Name = [] AmountF = ['5','20','0.5','2','1'] AmountV = ['0.25','0.2','14','6'] class Window(QtWidgets.QMainWindow): def __init__(self): super().__init__() centralWidget = QWidget() self.setCentralWidget(centralWidget) self.setGeometry(50, 50, 500, 500) self.dropdown = QComboBox() self.Open = QPushButton('Open') self.ListBox = QListWidget() self.dropdown.activated.connect(self.enter) self.Open.clicked.connect(self.open) self.show() layout = QGridLayout(centralWidget) layout.addWidget(self.dropdown) layout.addWidget(self.ListBox) layout.addWidget(self.Open) def open(self): name = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File') file = open(name[0], 'r') lines = file.readlines() for L in lines[0:6]: LI = L.replace('\n','') lines2.insert(0,LI) for L in lines[6:]: Li = L.replace('\n','') lines3.insert(0,Li) Name.insert(0,lines2[-1]) Name.insert(0,lines3[4]) del lines2[-1] del lines3[-1] for items in lines3: self.ListBox.insertItem(0, items) for item in Name: self.dropdown.insertItem(0, item) def enter(self): self.ListBox.clear() if self.dropdown.itemText(0) == Name[0]: for items in lines3: self.ListBox.insertItem(0, items) elif self.dropdown.itemText(1) == Name[1]: for item in lines2: self.ListBox.insertItems(0, item) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = Window() sys.exit(app.exec_()) 设置为keyboardType对我有用

编辑:

这仅在模拟器中有效,可以在仍显示本机键盘的实际设备上运行它。

在下面的示例中,

null封装在<TextInput />元素中。

<TouchableWithoutFeedback>

答案 5 :(得分:0)

最简单的解决方案是在 TextInput 上使用 onFocus 道具。

  1. 从“react-native”导入 Keyboard
<块引用>

导入 {Keyboard, TextInput} from '反应原生'

  1. 然后将 Keyboard.dismiss() 传递给 TextInput onFocus 道具,以阻止键盘在聚焦时弹出。
<块引用>

Keyboard.dismiss()} .../>

现在按下输入框来测试它是否会弹出键盘

答案 6 :(得分:-1)

我认为您需要添加以下内容:

<TextInput showSoftInputOnFocus={false} keyboardType="numeric" />