华为Harmony鸿蒙开发笔记十:剪切板

鸿蒙系统的剪切板都是由SystemPasteboard这个类来管理的,我们可以获取它的对象,来操作剪切板。

剪切板的数据都放在PasteData对象里,我们可以用pasteboard.getPasteData();获取PasteData对象。

    private SystemPasteboard pasteboard;
    pasteboard = SystemPasteboard.getSystemPasteboard(this);
    PasteData pasteData = pasteboard.getPasteData();

PasteData可以放入多条数据,每个数据为一个PasteData.Record节点,支持文本、html、UIR、Intent等

                    if (pasteboard != null) {
                        PasteData pasteData = new PasteData();
                        pasteData.addRecord(PasteData.Record.createPlainTextRecord("天王盖地虎"));
                        pasteData.addRecord(PasteData.Record.createPlainTextRecord("天王盖地虎"));
                        pasteData.addRecord(PasteData.Record.createHtmlTextRecord("<HTML>"));
                        Intent intent = new Intent();
                        Operation operation = new Intent.OperationBuilder()
                                .withBundleName("com.example.pasteboarddemo")
                                .withAbilityName("com.example.pasteboarddemo.MainAbility")
                                .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                                .build();
                        intent.setOperation(operation);
                        pasteData.addRecord(PasteData.Record.createIntentRecord(intent));
                        pasteboard.setPasteData(pasteData);
                    }

上面的代码在PasteData中添加了四个PasteData.Record节点,分别放置了文本、html、Intent,下面来读取

    private void getPasteboardData() {
        PasteData pasteData = pasteboard.getPasteData();
        if (pasteData == null) {
            return;
        }
        PasteData.DataProperty dataProperty = pasteData.getProperty();
        boolean hasHtml = dataProperty.hasMimeType(PasteData.MIMETYPE_TEXT_HTML);
        boolean hasText = dataProperty.hasMimeType(PasteData.MIMETYPE_TEXT_PLAIN);
        if (hasHtml || hasText) {
            for (int i = 0; i < pasteData.getRecordCount(); i++) {
                PasteData.Record record = pasteData.getRecordAt(i);
                String mimeType = record.getMimeType();
                if (mimeType.equals(PasteData.MIMETYPE_TEXT_HTML)) {
                    HiLog.info(LABEL_LOG, "getPasteboardData() 节点+"+i+"的内容="+record.getHtmlText().toString());
                } else if (mimeType.equals(PasteData.MIMETYPE_TEXT_PLAIN)) {
                    HiLog.info(LABEL_LOG, "getPasteboardData() 节点+"+i+"的内容="+record.getPlainText().toString());
                }else if (mimeType.equals(PasteData.MIMETYPE_TEXT_INTENT)) {
                    HiLog.info(LABEL_LOG, "getPasteboardData() 节点+"+i+"的内容="+record.getIntent().getBundle());
                }
            }
        }
    }

日志输出:

01-17 21:31:33.511 19048-19048/com.example.pasteboarddemo I 01100/My_log: getPasteboardData() 节点+0的内容=天王盖地虎
01-17 21:31:33.511 19048-19048/com.example.pasteboarddemo I 01100/My_log: getPasteboardData() 节点+1的内容=天王盖地虎
01-17 21:31:33.511 19048-19048/com.example.pasteboarddemo I 01100/My_log: getPasteboardData() 节点+2的内容=<HTML>
01-17 21:31:33.511 19048-19048/com.example.pasteboarddemo I 01100/My_log: getPasteboardData() 节点+3的内容=com.example.pasteboarddemo

另外,PasteData还有一个DataProperty属性对象,用来记录额外的信息,比如上面的数据中,可以获取当前的数据包含的类型:

            PasteData.DataProperty property=pasteData.getProperty();
            HiLog.info(LABEL_LOG, "getPasteboardData() pasteData.getMimeTypes()="+property.getMimeTypes().toString());

输出日志可以看出当前包含了文本、HTML、和Intent类型

01-17 21:31:33.511 19048-19048/com.example.pasteboarddemo I 01100/My_log: getPasteboardData() pasteData.getMimeTypes()=[text/plain, text/html, text/ohos.intent]

剪切板还有两个监听事件,变化事件和清空事件,设置方法虽然不一样,但是设置的对象却是一样的:

        IPasteDataChangedListener pasteDataChangedListener = new IPasteDataChangedListener() {
            @Override
            public void onChanged() {
                PasteData pasteData = pasteboard.getPasteData();
                if (pasteData == null) {
                    return;
                }
                HiLog.info(LABEL_LOG, "onChanged() ");
                // Operations to handle data change on the system pasteboard
            }
        };
        pasteboard.addPasteDataChangedListener(pasteDataChangedListener);
        pasteboard.removePasteDataChangedListener(pasteDataChangedListener);

https://download.csdn.net/download/y280903468/14504726

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值