Clipboard组件可以在iOS和Android的剪贴板中读写内容。

方法

static getString() #

获取剪贴板的文本内容,返回一个Promise你可以用下面的方式来调用。

async _getContent() { var content = await Clipboard.getString(); }

static setString(content: string) #

设置剪贴板的文本内容。你可以用下面的方式来调用。

_setContent() { Clipboard.setString('hello world'); }

例子

'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
  Clipboard,
  View,
  Text,
} = ReactNative;

var ClipboardExample = React.createClass({
  getInitialState() {
    return {
      content: 'Content will appear here'
    };
  },

  async _setClipboardContent(){
    Clipboard.setString('Hello World');
    try {
      var content = await Clipboard.getString();
      this.setState({content});
    } catch (e) {
      this.setState({content:e.message});
    }
  },

  render() {
    return (
      <View>
        <Text onPress={this._setClipboardContent} style={{color: 'blue'}}>
          Tap to put "Hello World" in the clipboard
        </Text>
        <Text style={{color: 'red', marginTop: 20}}>
          {this.state.content}
        </Text>
      </View>
    );
  }
});

exports.title = 'Clipboard';
exports.description = 'Show Clipboard contents.';
exports.examples = [
  {
    title: 'Clipboard.setString() and getString()',
    render() {
      return <ClipboardExample/>;
    }
  }
];

results matching ""

    No results matching ""