Copy with clipboard manager that supports old and new android versions?

后端 未结 1 1638
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 05:52

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

相关标签:
1条回答
  • 2020-12-03 06:48

    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");
    }
    
    0 讨论(0)
提交回复
热议问题