当前位置: 首页 > 知识库问答 >
问题:

Android剪贴板不复制文本

司徒鸿文
2023-03-14

我想在Android API上测试剪贴板,所以我开始使用Android的ClipboardManager和ClipData类创建一个简单的应用程序。我遵循了Android网站上的“复制和粘贴”指南,尽管我必须填写几个地方,因为该指南没有每行必要的代码(但我最终不得不注释掉Intent和URI粘贴方法,因为它没有给出具体的示例,因为我假设数据是多种多样的)。我运行了应用程序,但由于某种原因,我发送的文本似乎不起作用,或者显示不出来。有人知道这可能是什么原因吗?

这是我的课:

public class MainActivity extends Activity
{
// Creates a URI based on a base URI and a crecord ID based on the contact's last name
// Declares the base URI string
private static final String CONTACTS = "content://com.example.contacts";
// Declares a path string for the URIs that you use to copy data
private static final String COPY_PATH = "/copy";
// Declares the URI to paste to the clipboard
Uri copyURI;

// Declares a MIME type constant to match against the MIME types offered by the provider
private static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.example.contact";

private int pasteType;
private Menu menu;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    String lastName = "Nichols";
    copyURI = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName);
    setContentView(R.layout.activity_main);
    main();
}

void main()
{
    String text = "Hi, bob!";
    copyText(text);
    String data = paste();
    TextView tv = new TextView(this);
    tv.setText(data);
    setContentView(tv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    this.menu = menu;
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@TargetApi(11)
void copyIntent()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    Intent appIntent = new Intent(this, com.example.clipboard.MainActivity.class);
    ClipData clip = ClipData.newIntent("Intent", appIntent);
    clipboard.setPrimaryClip(clip);
    pasteType = 1;
}

@TargetApi(11)
void copyText(String text)
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("simple", text);
    clipboard.setPrimaryClip(clip);
    pasteType = 2;
}

@TargetApi(11)
void copyURI()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newUri(getContentResolver(), "URI", copyURI);
    clipboard.setPrimaryClip(clip);
    pasteType = 3;
}

@TargetApi(11)
String paste()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    /*if (pasteType == 1)
    {
        // Checks to see if the clip item contains an Intent, by testing to see if getIntent() returns null
        Intent pasteIntent = clipboard.getPrimaryClip().getItemAt(0).getIntent();
        if (pasteIntent != null)
        {

        }
    }*/
    if (pasteType == 2)
    {
        String pasteData = "";

        // Gets the ID of the "paste" menu item
        MenuItem mPasteItem = this.menu.findItem(R.id.paste);

        // If the clipboard doesn't conatin data, disable the paste menu item
        if (!(clipboard.hasPrimaryClip()))
            mPasteItem.setEnabled(false);
        else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)))
        {
            // This disables the paste menu item, since the clipboard has data, but it is not plain text
            mPasteItem.setEnabled(false);
        }
        else
            mPasteItem.setEnabled(true);

        // Examines the item on the clipboard. If getText() does not return null, the clip item contains the
        // text. Assume the application can only handle one item at a time.
        ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

        pasteData = (String) item.getText();
        return pasteData;
    }
    /*if (pasteType == 3)
    {

        ContentResolver cr = getContentResolver();
        ClipData clip = clipboard.getPrimaryClip();

        if (clip != null)
        {
            ClipData.Item item = clip.getItemAt(0);
            Uri pasteUri = item.getUri();
            // If the clipboard contains a URI reference
            if (pasteUri != null)
            {
                // Is this a content URI?
                String uriMimeType = cr.getType(pasteUri);
                // If the return value is not null, the Uri is a content URI
                if (uriMimeType != null)
                {
                    // Does the content provider offer a MIME type that the current application can use?
                    if (uriMimeType.equals(MIME_TYPE_CONTACT))
                    {
                        Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);
                        if (pasteCursor != null)
                        {
                            if (pasteCursor.moveToFirst()) {

                            }
                        }
                        pasteCursor.close();
                    }
                }
            }
        }
    }*/
    return null;
}
}

编辑:这是我的menu.xml文件,以帮助找出为什么我的菜单没有显示复制/粘贴选项。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />

</RelativeLayout>

共有1个答案

颛孙麻雀
2023-03-14

我认为你跟随的向导完全是一个误导!它缺少太多的信息。但是,我建议您简化代码:

  • 尝试评论一些块
  • 专注于仅处理文本,暂时忘记其余部分。

这里有一个很好的答案,可能会对你有所启发。

顺便说一句,据我所知,您的copy()和粘贴()方法完全没问题。您的问题与上下文菜单以及您如何处理它有关。

 类似资料:
  • 问题内容: 在我的Go语言命令行应用程序中,我需要能够使用Go将某些文本片段复制到系统剪贴板。基本上类似于PyperClip,但适用于Go。 我正在寻找与平台无关的解决方案!任何帮助将是巨大的:) 问题答案: 一个项目(仅适用于Windows和Mac)似乎正在接近您想要的:。 提供复制和粘贴到剪贴板的Go。 剪贴板_linux.go类中提供Linux支持:系统命令的简单包装。 另一种方法:尝试利用

  • 嘿,我正在尝试将文本从回收器视图项目复制到剪贴板中,当我尝试在活动中执行此操作时,它的工作原理,但当我尝试查看者中的代码时,我得到了未解决的参考:CLIPBOARD_SERVICE错误这里是代码:

  • 问题是,从vim到剪贴板的复制/粘贴停止工作。我不知道为什么。。。 在中,我也没有看到和缓冲区... 这是我的。vimrc,如果需要的话。 有什么建议吗? 谢谢

  • 问题内容: 如何将BufferedImage存储到系统剪贴板中? 问题答案: 这是从这里获得的工作代码,已成功测试

  • 将一个字符串复制到剪贴板。 仅作为用户操作的结果(即,在 click 事件侦听器中)。 创建一个新的 <textarea> 元素,用提供的数据填充它,并将其添加到 HTML 文档中。 使用 Selection.getRangeAt() 来存储选择的范围(如果有的话)。 使用 document.execCommand('copy') 复制到剪贴板。 从HTML文档中删除 <textarea> 元素。

  • 我有一个HTML格式的表,我看到有人复制/粘贴该表的部分内容。当我尝试它的时候,结果是一团糟,需要大量的清理,因为表中包含了大量带有图像和其他东西的列。 有没有办法将选择限制在表的前2列? 有没有办法替换正在复制的文本(用户选择“Apple”并按下复制,但“Banana”最终出现在剪贴板中)?