如何使电子邮件地址可点击?

46

我在我的应用程序中有一些文字,上面写着如果您需要额外的帮助,请通过电子邮件联系我们并提供电子邮件地址。

但我希望他们能够单击电子邮件链接并打开他们的邮件客户端。这是可能的吗?或者这是不好的做法吗?

如果这是合理的做法,如何实现?

5个回答

112

这是一个非常合理的请求,Linkify类可以将每个电子邮件地址转换为适当的链接。只需将autoLink属性添加到您的XML中:

<TextView
    ...
    android:autoLink="email" />

2
最佳解决方案...将自动在您的“TextView”中查找电子邮件地址,因此如果您想要在文本中放置一个段落,它将自动转换所有电子邮件地址。 - Jason Robinson
1
如何在这种情况下去除链接的下划线? - Hossein Asmand

9
您可以使用setTextOnClickListener在文本上实现可点击效果。
textView.setOnClickListener(new View.OnClickListener());

您可以创建一个新的Intent,使用ACTION_SEND打开电子邮件客户端。像这样设置类型、电子邮件地址和主题:

Intent emailintent = new Intent(android.content.Intent.ACTION_SEND);
emailintent.setType("plain/text");
emailintent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {"mailk@gmail.com" });
emailintent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
emailintent.putExtra(android.content.Intent.EXTRA_TEXT,"");
startActivity(Intent.createChooser(emailintent, "Send mail..."));

那并没有回答问题,因为整个“TextView”都将是可点击的。只有电子邮件地址应该是一个链接。 - Marc Plano-Lesay

6

您需要在onClickListener中触发一个意图:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain"); // send email as plain text
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));

3
请注意,从API 24开始有一个小错误,如果电子邮件地址的本地部分正好有两个字符,例如"it@google.com",则已接受的解决方案将无法使用。
请查看问题:https://issuetracker.google.com/issues/64435698 据称已经修复,但显然尚未推出。 (你难道不喜欢他们知道这个问题却连文档都不更新吗?https://developer.android.com/reference/android/widget/TextView.html#attr_android:autoLink)
因此,除非你确定不涉及这种2个字符的电子邮件地址,否则你应该暂时使用这里接受的方法: TextView to send email when clicked 请注意从TextView中删除autolink属性。

0

接受的答案可能适用于电子邮件,但如果您想检测不同的模式,如电子邮件、联系电话、网址,并为这些模式设置单独的点击实现,我建议您使用CustomClickableEmailPhoneTextview

enter image description here

使用该库的示例代码。

CustomPartialyClickableTextview customPartialyClickableTextview= (CustomPartialyClickableTextview) findViewById(R.id.textViewCustom);

                /**
                 * Create Objects For Click Patterns
                 */
                ClickPattern email=new ClickPattern();
                ClickPattern phone=new ClickPattern();
                ClickPattern weblink=new ClickPattern();

                /**
                 * set Functionality for what will happen on click of that pattern
                 * In this example pattern is email
                 */
                email.setOnClickListener(new ClickPattern.OnClickListener() {
                    @Override
                    public void onClick() {

                        Toast.makeText(MainActivity.this,"email clicked",Toast.LENGTH_LONG).show();


                    }
                });

                /**
                 * set Functionality for what will happen on click of that pattern
                 * In this example pattern is phone
                 */
                phone.setOnClickListener(new ClickPattern.OnClickListener() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this,"phone clicked",Toast.LENGTH_LONG).show();

                    }
                });

                /**
                 * set Functionality for what will happen on click of that pattern
                 * In this example pattern is weblink
                 */
                weblink.setOnClickListener(new ClickPattern.OnClickListener() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this,"website clicked",Toast.LENGTH_LONG).show();

                    }
                });

                /**
                 * set respective regex string to be used to identify patter
                 */
                email.setRegex("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"); // regex for email
                phone.setRegex("[1-9][0-9]{9,14}"); // regex for phone number
                weblink.setRegex("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"); // regex for weblink

                /**
                 * add click pattern to the custom textview - first parameter is tag for reference second parameter is ClickPattern object
                 */
                customPartialyClickableTextview.addClickPattern("email",email);
                customPartialyClickableTextview.addClickPattern("phone",phone);
                customPartialyClickableTextview.addClickPattern("weblink",weblink);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接