[React Native]访问操作系统剪贴板 Clipboard
发布时间:2020-12-15 08:30:21 所属栏目:百科 来源:网络整理
导读:我们之前学习了TextInput组件,有时候我们需要在TextInput组件中复制或者粘贴一些文字。 React Native为开发者提供了 Clipboard API,Clipboard 组件可以在iOS和Android的剪贴板中读写内容。目前还只支持获取或者存放字符串。 主要方法 static getString() 获
我们之前学习了TextInput组件,有时候我们需要在TextInput组件中复制或者粘贴一些文字。 主要方法static getString() static setString(content: string) 官方例子代码比较简单,直接展示官方例子: import React,{Component} from 'react';
import {
AppRegistry,StyleSheet,View,Text,Clipboard
} from 'react-native';
class AwesomeProject extends Component {
state = {
content: 'Content will appear here'
};
//异步函数 箭头函数不需要绑定this了
_setClipboardContent = async () => {
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',marginTop:100}}>
Tap to put "Hello World" in the clipboard
</Text>
<Text style={{color: 'red',marginTop: 20}}>
{this.state.content}
</Text>
</View>
);
}
}
AppRegistry.registerComponent('AwesomeProject',() => AwesomeProject);
运行结果: 更多精彩请关注微信公众账号likeDev (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |