与支持旧的和新的Andr​​oid版本剪贴板管理复制? [英] Copy with clipboard manager that supports old and new android versions?

查看:111
本文介绍了与支持旧的和新的Andr​​oid版本剪贴板管理复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式复制文本在Android上,在另一个问题上最投票的答案提供了这些行,但在使用它们时,我得到错误:类需要API级别11(目前最小为8):

I'm trying to copy text programatically on android, the most voted answer on another question provided these lines but when using them I get error: Class requires API level 11 (current min is 8):

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip);

我直接从问题中复制的行。经过与尝试 进口android.content.ClipboardManager; 我测试进口android.text.ClipboardManager; 键,但它也产生了误差的方法setPrimaryClip(ClipData)是不确定的约ClipboardManager是德precated类型ClipboardManager 加上警告。

I copied the lines directly from the question. After trying with import android.content.ClipboardManager; I tested import android.text.ClipboardManager; and but it produced an error too The method setPrimaryClip(ClipData) is undefined for the type ClipboardManager plus warnings about ClipboardManager being deprecated.

我的应用程序,它支持的Andr​​oid 2.2(API 8我想)起,我怎么可以复制文本,它适用于所有版本的Andr​​oid?

My app that supports Android 2.2 (API 8 I think) onwards, how can I copy text so it works on all versions of android?

推荐答案

尝试使用类似以下内容:

Try to use something like the following:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    final android.content.ClipData clipData = android.content.ClipData
            .newPlainText("text label", "text to clip");
    clipboardManager.setPrimaryClip(clipData);
} else {
    final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setText("text to clip");
}

这篇关于与支持旧的和新的Andr​​oid版本剪贴板管理复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆