【问题标题】:Copy and share text into Clipboard from listview从 listview 将文本复制并共享到剪贴板
【发布时间】:2014-09-24 21:33:55
【问题描述】:

我正在开发一个基本的 Android 应用程序,它使用 SAXParser 从资产中解析 xml 文件,并且我已经成功解析了 xml。

然后我使用以下代码为 Context 注册已解析的文本:

.......

this.registerForContextMenu(content);


@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Choose");
    menu.add(0, v.getId(), 0, "Copy");
    menu.add(0, v.getId(), 0, "Share");

    if (selectedText.size() > 0) {
        menu.add(0, v.getId(), 0, "Copy");
        menu.add(0, v.getId(), 0, "Share");
    }

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getTitle() == "Copy") {
        copySelectedText(getSelectedText());
        return true;
    } else if (item.getTitle() == "Share") {
        shareSelectedText(getSelectedText());
        return true;
    } else
        return false;
}

public boolean copySelectedText(String text) {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newIntent(position, getIntent());
    clipboard.setPrimaryClip(clip);
    return true;
}

private void shareSelectedText(String text) {
    copySelectedText(null);

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Shared from MyApp");
    startActivity(Intent.createChooser(sharingIntent,
            "Sharing text using"));
}

public String getSelectedText() {

    //How do I code here?? }

    return null;
}

使用上面的代码,当我长按已解析文本的任何部分时,上下文菜单可以正常工作。但我的问题是,我不知道将解析后的文本放入剪贴板。有人可以帮帮我吗?提前致谢。

【问题讨论】:

    标签: android xml parsing copy clipboard


    【解决方案1】:

    我已经解决了我的问题。这是我的做法。

    我从 assets 文件夹中解析了一个 xml 文件,并将结果传递到 listview 数组中。我最初的问题是如何在长按文本时复制文本。但现在我以某种方式设法解决了我的问题,我在这里分享它。希望它可以帮助有类似问题的人。

    @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Choose");
    menu.add(Menu.NONE, CONTEXTMENU_COPY, 0, "Copy");
    menu.add(Menu.NONE, CONTEXTMENU_SHARE, 1, "Share");
    }
    
    
    @Override public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    
    case CONTEXTMENU_COPY:
    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
    
    //The textview which holds the parsed xml
    CharSequence selectedTexts = ((TextView)menuInfo.targetView.findViewById(R.id.text)).getText();
    ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("Intent", (CharSequence) selectedTexts);
    clipboard.setPrimaryClip(clip);
    Toast.makeText(getApplicationContext(), "Selected text is copied",Toast.LENGTH_SHORT).show();
    break;
    
    case CONTEXTMENU_SHARE:
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    CharSequence textToShare = ((TextView) info.targetView.findViewById(R.id.text)).getText();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
    shareIntent.setType("text/plain");
    startActivity(Intent.createChooser(shareIntent, "Hiaite zangin share in"));
    }
    return super.onContextItemSelected(item); }
    

    在识别出哪个项目被长按之后,我需要做的就是这行代码!!

    CharSequence textToShare = ((TextView) info.targetView.findViewById(R.id.text)).getText();
    

    这太简单了,我可以踢自己。我希望它对像我这样苦苦挣扎的新手有所帮助,并为某人省去很多初学者的头痛! :P

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多