【问题标题】:Copy Text of a Text Field复制文本字段的文本
【发布时间】:2014-05-05 15:22:55
【问题描述】:

我有一个文本字段和一个按钮。我会在按下按钮后立即复制要复制的文本字段的文本。如何才能做到这一点?当我搜索它 (How to copy text programmatically in my Android app?) 时,我读到了 ClipboardManager 方法,但有传言说它也已被弃用。我应该避免吗?谢谢

【问题讨论】:

标签: android eclipse copy textfield


【解决方案1】:

Honeycomb 已弃用 android.text.ClipboardManager 并引入了 android.content.ClipboardManager

您应该检查android.os.Build.VERSION.SDK_INT 是否至少为android.os.Build.VERSION_CODES.HONEYCOMB,因此请使用其中一个。

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
    {
        // Old clibpoard
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText("the text");
    } 
    else
    {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clipData = android.content.ClipData.newPlainText("PlainText", "the text");
        clipboard.setPrimaryClip(clipData);
    }       

【讨论】:

  • 这个答案与我的答案@matiash 非常相似 ..请在发布新答案之前检查其他答案..
  • 我回答了一个较短的版本,然后添加了代码。对不起。
  • 没关系@matiash ..我只是指出..不需要sry和所有..无论如何我都会赞成你的答案..
【解决方案2】:

试试这个link..它的答案

 int sdk = android.os.Build.VERSION.SDK_INT;
            if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setText("text to clip");
            } else {
                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
                android.content.ClipData clip = android.content.ClipData.newPlainText("text label","text to clip");
                clipboard.setPrimaryClip(clip);
            }

【讨论】:

    【解决方案3】:

    Copy/Paste Offical Tutoiral 看看吧

    相关代码:

    ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);
    Copy the data to a new ClipData object:
    For text
    
    // Creates a new text clip to put on the clipboard
     ClipData clip = ClipData.newPlainText("simple text","Hello, World!");
    

    【讨论】:

    • 请包含您提供的链接中的相关信息。
    猜你喜欢
    • 1970-01-01
    • 2014-04-27
    • 2014-04-30
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2013-10-23
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多