系统键盘上的粘贴板内容点击在RichEditor内显示无法被拦截?

使用RichEditer控件,复制了一段文本,在粘贴到输入框内有2种方式,

1、长按粘贴 (该方式可以被拦截到,并根据业务场景处理分别显示文字和图片)

2、系统键盘上还有个剪贴板,点击后直接就在RichEditer那显示内容,走aboutToIMEInput和onIMEInputComplete回调方法,无法根据业务逻辑自己处理需要展示图片的内容

期望可以解决第二种的场景

阅读 780
1 个回答
import pasteboard from '@ohos.pasteboard';
import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';
export class PasteboardUtils{
//监听剪切板
onDataChange(){
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
let listener = () => {
this.offDataChange()
this.changeData()
console.info('The system pasteboard has changed.');
};
systemPasteboard.on('update', listener);
}
//取消监听
offDataChange(){
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
let listener = () => {
console.info('The system pasteboard has changed.');
};
systemPasteboard.off('update', listener);
}
// 获取剪切板数据
onGetData():string{
let text: string =''
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => {
if (err) {
console.error('Failed to get PasteData. Cause: ' + err.message);
return;
}
text= pasteData.getPrimaryText();
// let pixelMap: image.PixelMap = pasteData.getPrimaryPixelMap();
console.log("success in get data "+ text);
// console.log("success in get data "+ pixelMap);
});
return text;
}
// 向剪切板添加数据
onSetData(data: string){
let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, data);
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.setData(pasteData, (err, data) => {
if (err) {
console.error('Failed to set PasteData. Cause: ' + err.message);
return;
}
console.info('Succeeded in setting PasteData.'+data);
});
}
// 修改数据
changeData(){
let text: string =''
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => {
if (err) {
console.error('Failed to get PasteData. Cause: ' + err.message);
return;
}
(text as string) = pasteData.getPrimaryText();
console.log("success in get data "+ text);
// 图文混排可用PixelMap
// let pixelMap: image.PixelMap = pasteData.getPrimaryPixelMap();
// console.log("success in get data "+ pixelMap);
(text as string)= text.toString()+'testtag'
this.onSetData(text)
});
}
}

上述demo通过on监听剪切板,getData获取剪切板数据,setData向剪切板添加数据处理后的数据。可使用pixelmap存储图文类型。剪切板参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-pasteboard-0000001774121662\#ZH-CN\_TOPIC\_0000001774121662\_\_getprimarypixelmap9

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进