简体   繁体   English

禁用 React-Native 文本输入选项

[英]Disable Options on React-Native Text Input

I am using TextInput for a project and wanted to DISABLE any kind of text selection or actions like (cut/copy/paste/share) as shared in the screenshot below.我正在为一个项目使用 TextInput,并希望禁用任何类型的文本选择或操作,例如(剪切/复制/粘贴/共享),如下面的屏幕截图中所共享。

I am not able find anything in the react-native official documentation我在 react-native 官方文档中找不到任何内容

在此处输入图片说明

You should add 2 attributes selectTextOnFocus and editable您应该添加 2 个属性selectTextOnFocuseditable

For example:例如:

<TextInput editable={false} selectTextOnFocus={false} />

contextMenuHidden is to disable the user from pasting text into certain fields and to hide context menu. contextMenuHidden是禁止用户将文本粘贴到某些字段并隐藏上下文菜单。

Update: This hasn't been included in a release yet.更新:这尚未包含在版本中。 You can always see what release any commit is in by clicking on the link and looking at the tags.您始终可以通过单击链接并查看标签来查看任何提交所在的版本。 so I wouldn't expect it to be on a stable release until 0.55.所以我不希望它在 0.55 之前稳定发布。

<TextInput contextMenuHidden={true} />

Check the commit here: Add option to hide context menu for TextInput在此处检查提交: 添加选项以隐藏 TextInput 的上下文菜单

只需给你的 textinput 属性editable={false}

Set pointerEvents to none on parent View of TextInput to disable touch events, consider following example:TextInputView 上将 pointerEvents设置为 none 以禁用触摸事件,请考虑以下示例:

<View pointerEvents="none">
  <TextInput ... />
</View>

您可以使用 View 并使用 removeClippedSubviews={true} (适用于 Android)并使用 contextMenuHidden={true} (适用于 IOS)

<View removeClippedSubviews={true}> <TextInput contextMenuHidden={true} /> </View>

Use caretHidden={true} if you want to disable all operation like Cut Paste Copy.如果要禁用所有操作(如剪切粘贴复制),请使用caretHidden={true} It will also hide your cursor as well它也会隐藏您的光标

This trick worked for me.这个技巧对我有用。 Here I am using NativeBase.这里我使用的是 NativeBase。 Keep this <TextInput> inside a <Item> tag.将此<TextInput>保留在<Item>标签内。 Now the selection property should not work.现在选择属性应该不起作用。

code sample attached below.下面附上代码示例。

<Item>
<Input
  value={this.props.call_state.destination}
  onChangeText={text => this.props.setDestination(text)}
  returnKeyType={"done"}
  keyboardType={"numeric"}
/>
</Item>

You should install nativebase first and then import {Item} from native-base in your component您应该先安装 nativebase,然后在组件中从 native-base 导入{Item}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM