响应原生文本输入禁用所选文本的输入

时间:2017-03-18 09:58:38

标签: javascript reactjs web react-native

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

enter image description here

有人可以帮我这个吗?

4 个答案:

答案 0 :(得分:3)

您可以将属性设置为editable,并选择TextOnFocus为false

像:

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

答案 1 :(得分:0)

只需将选择设置为文本末尾: selection = {{start:this.state.text.length,end:this.state.text.length}}

答案 2 :(得分:0)

我想我有两个想法

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

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

  3. 我希望我的想法可以帮助你...

答案 3 :(得分:0)

您可以使用选择属性。然后,当选择更改时,将选择道具设置为选择的结束位置。

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}
      />
    );
  }
}