问题
I tried to share a multi-line text using the following code, but only the last line appears.
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.setType("text/plain")
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Found this cool deal! Check it out.")
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, TITLE)
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "MRP : $PRICE")
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Deal Price : $DEAL_PRICE")
startActivity(Intent.createChooser(sharingIntent, "Share using"))
回答1:
When you call putExtra(key, value)
, any value previous put under the same key
gets wiped out. Try putting a single string that contains all the text you want:
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Found this cool deal! Check it out.\n" +
TITLE + "\n" +
"MRP : $PRICE\n" +
"DEAL PRICE : $DEAL_PRICE");
As for the HTML content, you'll need to show us the code you used. However, you probably don't want to use fromHtml()
; that converts HTML to styled text, which I suspect is not what WhatsApp expects to receive. Try just sending the raw HTML with the correct MIME type.
回答2:
The reason that only the last line appears is that a single intent can only have one value for a key. So each time you call putExtra(EXTRA_TEXT,foo), you're overwriting the previous one. If you want to send multiple lines, send it as a single string with '\n' characters.
As for sending html- you'd need to tell it that you're sending html. You're telling it you're sending plain text. Change the mime type to text/html if you're sending html. (Note: I have no idea if whatsapp supports html or not, but that's the way you'd send html to any app that does support it).
回答3:
In Kotlin you can use tripple quote martks for multilined text, I'm not sure if you can in Java.
val moreStuff = "Text or Numbers or arrays"
var myText = """
line 1 of the text
line 2 of the text
$moreStuff
"""
It will format it the way you type it
var myText = """
Found this cool deal! Check it out.
$TITLE
MRP : $PRICE
DEAL PRICE : $DEAL_PRICE)"""
来源:https://stackoverflow.com/questions/48039651/whats-app-multiline-text-share-intent