您应该添加2个属性selectTextOnFocus
和editable
例如:
<TextInput editable={false} selectTextOnFocus={false} />
contextMenuHidden 用于禁止用户将文本粘贴到特定字段中并隐藏内容菜单。
更新: 这项功能尚未包含在发布版本中。您可以通过点击链接查看任何提交所在的版本。因此,我不希望看到它被稳定发布,直到0.55版。
<TextInput contextMenuHidden={true} />
在这里检查提交: 为TextInput添加隐藏上下文菜单的选项
只需将您的文本输入框添加属性 editable={false}
即可。
将TextInput
的父级View
的pointerEvents设置为none以禁用触摸事件,可以参考以下示例:
<View pointerEvents="none">
<TextInput ... />
</View>
caretHidden={true}
。 - Pir Shukarullah Shah您可以使用一个View,并使用removeClippedSubviews={true}(适用于Android),并使用contextMenuHidden={true}(适用于IOS)
<View removeClippedSubviews={true}>
<TextInput contextMenuHidden={true} />
</View>
参考以下问题 https://github.com/facebook/react-native/issues/33697
const App = () => {
const [selection, setSelection] = useState(null);
const [text, setText] = useState('');
return (
<TextInput
selection={selection}
onSelectionChange={(e) => setSelection(e.nativeEvent.selection)}
value={text}
onChangeText={(text) => setText(text)}
style={{ alignSelf: 'center', width: 200, marginTop: 50, backgroundColor: 'grey' }}
/>
);
};
const [text1, setText1] = useState('')
const clearClipboard = () =>{
Clipboard.setString('')
}
const onChangeText = (text) =>{
//For android 12+ clipboard autofill option, dont allow text change more than one char..which means user is typing.
if(text1.length+1 >= text.length){
setText1(text)
}
}
<TextInput onChangeText={onChangeText} value={text1} contextMenuHidden={true} onTouchEnd={clearClipboard}></TextInput>
如果您想禁用所有操作(如剪切、复制和粘贴),请使用 caretHidden={true}。它也将隐藏您的光标。
这个技巧对我很有用。在这里,我使用了NativeBase。将<TextInput>
放在<Item>
标签中。现在选择属性不应该起作用。
以下是代码示例。
<Item>
<Input
value={this.props.call_state.destination}
onChangeText={text => this.props.setDestination(text)}
returnKeyType={"done"}
keyboardType={"numeric"}
/>
</Item>
您应该先安装NativeBase,然后在组件中从NativeBase导入{Item}