【发布时间】:2020-12-28 13:45:44
【问题描述】:
我的申请中有一些文字说如果您需要额外的帮助,请给我们发电子邮件,这是电子邮件地址,等等。
但我希望他们能够单击电子邮件链接并打开他们的电子邮件客户端。那可能吗?或者这是不好的做法?
如果是合理的做法,怎么做?
【问题讨论】:
标签: android
我的申请中有一些文字说如果您需要额外的帮助,请给我们发电子邮件,这是电子邮件地址,等等。
但我希望他们能够单击电子邮件链接并打开他们的电子邮件客户端。那可能吗?或者这是不好的做法?
如果是合理的做法,怎么做?
【问题讨论】:
标签: android
您可以通过在文本上使用 setOnClickListener 来使您的文本可点击
textView.setOnClickListener(new View.OnClickListener());
您可以通过使用 ACTION_SEND 创建新 Intent 来打开电子邮件客户端。 Settype,电子邮件地址和主题如下:
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 将是可点击的。只有电子邮件地址应该是一个链接。
您需要在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, ""));
【讨论】:
请注意从 API 24 开始的一个小错误,如果电子邮件地址的本地部分正好有 2 个字符(如“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 中删除自动链接属性。
【讨论】:
接受的答案可能适用于电子邮件,但如果您想检测不同的模式,如电子邮件、联系电话、网络链接并为这些模式设置单独的点击实现,我建议您使用CustomClickableEmailPhoneTextview
使用该库的示例代码。
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);
【讨论】: