[英]How to make XML string resource email clickable with predefined Subject and Body on TextView
I am trying to achieve a solution where on click of email id in strings XML containing HTML tags we should open Intent Chooser of email apps available in the system, then in the email compose screen mailTo , subject & the body should be populated.我正在尝试实现一个解决方案,在单击包含 HTML 标签的字符串 XML 中的 email id 时,我们应该打开系统中可用的 email 应用程序的 Intent Chooser,然后在 email 撰写屏幕mailTo中,应填充主题和正文。
to - recipient@xyz.com subject - xyz.... body - xyz.....
I have tried using android:autoLink="email"
attribute, it is making the email clickable but it only pre-populates the recipient's email id and nothing else.我试过使用android:autoLink="email"
属性,它使 email 可点击,但它只预填充收件人的 email id,仅此而已。
Is there any other way around to achieve the required solution, without using a third-party library?有没有其他方法可以在不使用第三方库的情况下实现所需的解决方案?
You have to add a link as follow:您必须添加一个链接,如下所示:
mailto:recipient@xyz.com?subject=xyz subject&body=xyz body
This link will open an Intent for Mail application and will contain a prefill subject and body.此链接将打开 Intent for Mail 应用程序并将包含预填主题和正文。 (Ref: https://css-tricks.com/snippets/html/mailto-links/ ) (参考: https://css-tricks.com/snippets/html/mailto-links/ )
You can have a string as follow:你可以有一个字符串如下:
<string name="link_str"><a href="mailto:recipient@xyz.com?subject=xyz subject&body=xyz body">Send email</a> </string>
And then make TextView linkable as specified in https://stackoverflow.com/a/49694038/5309486然后按照https://stackoverflow.com/a/49694038/5309486中的规定使 TextView 可链接
Namaste to all,向所有人合十礼,
Below is the working solution, without using the android:autoLink="email"
attribute.下面是工作解决方案,不使用android:autoLink="email"
属性。
strings.xml字符串.xml
<string name="str_email_included">Mail us at - <br/> <a href=\'mailto:hello@xyz.com?subject=Subject1&body=Body1\'>hello@xyz.com</a></string>
Java Java
textView.setText(Html.fromHtml(getString(R.string.str_email_included))); textView.setMovementMethod(LinkMovementMethod.getInstance());
Kotlin Kotlin
textView.text = HtmlCompat.fromHtml(getString(R.string.str_email_included), HtmlCompat.FROM_HTML_MODE_LEGACY) textView.movementMethod = LinkMovementMethod.getInstance()
The above movementMethod
does the job, so onClick of email id, Intent Chooser will open and the mailTo, subject & body get pre-populated as required.上面的movementMethod
完成了工作,所以 email id 的 onClick,Intent Chooser 将打开,并且 mailTo、subject 和 body 会根据需要预先填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.