strings.xml文件中的超链接电子邮件地址?

时间:2014-11-14 20:20:37

标签: android string email

如何超链接电子邮件地址,以便在点击时将用户从strings.xml中收集到撰写的电子邮件。在我的strings.xml文件中,我使用以下内容超链接正常URL的文本:

<![CDATA[<a href="http://google.com">Google website example</a>]]>

我尝试使用类似的电子邮件技术,但没有发生任何事情。

<![CDATA[<a href="mailto:example@gmail.com">Email example</a>]]>

我做错了什么?提前致谢

2 个答案:

答案 0 :(得分:1)

您可以使用Linkify课程。 您将在xml文件中包含常规字符串,例如:

<string name="myemail">something@gmail.com</string>

然后在你的代码中:

String message="Some message ";
message=message+getResources().getText(R.string.myemail);
final SpannableString mes = new SpannableString(message);
Linkify.addLinks(mes, Linkify.EMAIL_ADDRESSES);

//TextView       
final TextView tx1=new TextView(this);
tx1.setText(mes);
tx1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

然后,在textview中,您将在电子邮件文本上有一个可点击的链接。

答案 1 :(得分:0)

您可以使用其他方式,您可以在字符串中使用基本HTML,如示例

所示
<string name="send_mail"><![CDATA[ <a href="mailto:example@mail.com">email us</a>]]></string>

然后创建AlertDialog,如示例

所示
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Data")
    .setIcon(R.drawable.info)
   .setMessage(Html.fromHtml(R.string.send_mail)
   .setCancelable(true)
   .setNegativeButton("OK",null);

AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

有关详细信息,请查看此问题Link