以共享意图传递多个文本/纯文本值 [英] Passing Multiple text/plain values in share intent

查看:65
本文介绍了以共享意图传递多个文本/纯文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用android创建一个记事本应用程序.我提供了与其他应用共享个人笔记的功能. 我希望共享功能共享笔记的标题和内容.我似乎无法使其正常工作. 以下是我用于共享意图的Java代码.

I am creating a notepad application in android. I have given a functionality to share individual note with other apps. I want the share function to share the title and content of the note. I can't seem to make it work. Below is my java code for share intent.

JAVA

    String title=noteModel.getTitle();
    String content=noteModel.getContent();
    Intent intentShare = new Intent();
    intentShare.setAction(Intent.ACTION_SEND);

    intentShare.putExtra(Intent.EXTRA_TEXT,title);
    intentShare.putExtra(Intent.EXTRA_TEXT,content);            
    intentShare.setType("text/plain");
    context.startActivity(intentShare.createChooser(intentShare,"Send note to"));

推荐答案

我找到了一种只用一个EXTRA_TEXT传递多个字符串的方法. 我想传递两个值,即title和content,因此我将title的值存储在名为"title"的字符串中,并将content的值存储在名为"content"的字符串中. 现在,把戏!我将两个字符串连接在一起,并将该字符串存储为新字符串,然后将该字符串传递到EXTRA_TEXT中.

I found a way to pass multiple strings with just one EXTRA_TEXT. I wanted to pass two values, namely, title and content, So I stored the value of title in a string called "title" and the value of content in a string called "content". Now, the trick ! I concatenated both the strings together and stored that concatenated string to a new string and passed that string into EXTRA_TEXT.

我将分享我的正确代码,只是为了给自己一个清晰的图片.

I am gonna share my correct code just to give a clear picture.

String title=noteModel.getTitle();
String content=noteModel.getContent();
String titleAndContent="Title: "+title+"\n Content: "+content;

Intent intentShare = new Intent();
intentShare.setAction(Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare.putExtra(Intent.EXTRA_TEXT,titleAndContent);

context.startActivity(intentShare);

这篇关于以共享意图传递多个文本/纯文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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