【问题标题】:react native text input disable selection of text entered反应本机文本输入禁用选择输入的文本
【发布时间】:2017-08-09 22:01:51
【问题描述】:

我有一个在 React Native 中使用的文本输入,并且想要禁用文本选择和图像中显示的其他选项:-

有人可以帮我解决这个问题吗?

【问题讨论】:

标签: javascript reactjs web react-native


【解决方案1】:

你可以设置属性editable和selectTextOnFocus为false

喜欢:

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

【讨论】:

    【解决方案2】:

    只需将选择设置到文本的末尾: 选择={{开始:this.state.text.length,结束:this.state.text.length}}

    【讨论】:

      【解决方案3】:

      我想我有两个想法

      1. 您可以尝试在属性中设置selectTextOnFocus = "false" clearTextOnFocus = "true" 从组件TextInput:

      2. 如果您的 TextInput 是焦点,您可以使用 onChangeText 创建一个删除文本的方法。

      希望我的想法能帮到你……

      【讨论】:

        【解决方案4】:

        您可以使用选择属性。 And when selection change, set selection prop to end position of your selection.

        class App extends React.Component {
          constructor(props) {
            super(props);
            this.state = {
              selection: { start: 0, end: 0 },
            };
          }
        
          onSelectionChange = ({ nativeEvent: { selection, text } }) => {
            this.setState({ selection: { start: selection.end, end: selection.end } });
          };
        
          render() {
            return (
              <TextInput
                onSelectionChange={this.onSelectionChange}
                selection={this.state.selection}
                value={this.state.value}
              />
            );
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-11
          • 1970-01-01
          相关资源
          最近更新 更多