在 TextInput 中启用粘贴和选择 - React Native

Wal*_*cke 6 react-native

我正在尝试在我的 TextInput 中实现复制和粘贴,但似乎无法实现。当我长按 TextInput 时,我期待着一个工具提示,但没有任何反应。

我了解剪贴板并知道如何实现它,但我似乎无法将粘贴选项弹出给用户。

我有什么想法可以实现这一目标吗?

<TextInput
                maxLength={29}
                autoCapitalize={'characters'}
                numberOfLines={1}
                keyboardType={'default'}
                underlineColorAndroid='transparent'
                autoCorrect={false}
                value={IBAN.printFormat(this.state.ibanInput)}
                returnKeyType={'next'}
                onChangeText={iban => this.verifyIban(iban)}
                style={[{ borderWidth: 1, borderRadius: 2, height: '100%', width: '100%', textAlign: 'center', fontSize: width/24 },

                ]}
              />
Run Code Online (Sandbox Code Playgroud)

Jit*_*har 2

如果复制/粘贴不适用于 TextInput - React Native,这里是答案

步骤 1)在构造函数中采用 testWidth 属性并将值指定为“99%”。

例如

this.state = {testWidth: '99%' };

步骤 2)在 componentDidMount 中更改 testWidth 值,如“100%”,在 setTimeout 内执行。

例如

 setTimeout(() => {
      this.setState({ testWidth: '100%' })
    }, 100)
Run Code Online (Sandbox Code Playgroud)

步骤3)在TextInput的style属性中添加我们在Contractor中声明的动态宽度,例如

<TextInput style={{ width: this.state.testWidth }} />
Run Code Online (Sandbox Code Playgroud)

这是完整的代码:(只需复制并粘贴到 App.js 文件中)。

import React, { Component } from 'react';
    import { TextInput, View } from 'react-native';

    export class App extends Component {
      constructor(props) {
        super(props);
        this.state = { text: '', testWidth: '99%' };
      }
      componentDidMount() {

        setTimeout(() => {
          this.setState({ testWidth: '100%' })
        }, 100)
      }
      render() {
        return (
          <View style={{ marginTop: 50 }}>
            <TextInput
              style={{ width: this.state.testWidth }}
              placeholder="Type here to translate!"
              onChangeText={(text) => this.setState({ text })}
              value={this.state.text}
            />
          </View>
        );
      }
    }
Run Code Online (Sandbox Code Playgroud)

祝你好运