我有一个包含电子邮件和电话号码的文本视图。我想要的效果是,当用户点击电子邮件时,它将打开默认的电子邮件应用程序,而当用户点击电话号码时,该应用程序会将其拉到拨号器中。使用以下代码:
XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/careers_guidance"
android:id="@+id/careers_guidance_text"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:linksClickable="true"
android:textColorLink="#0000EE"
android:autoLink="all"/>
Java:
careersGuidance = view.findViewById(R.id.careers_guidance_text);
careersGuidance.setText(Html.fromHtml("<p>Help with choosing the right course and with thinking about where this might take you in the future.</p>" +
"<p>Tel: <a href=\"tel:01274433043\">01274 433043</a></p>Email: <a href=\"mailto:careers@bradfordcollege.ac.uk\">careers@bradfordcollege.ac.uk</a>"));
当我运行我的应用程序时,只有电子邮件是可单击的,并且按照我希望的方式工作,电话号码却不可单击或突出显示。
但是,我注意到,如果我从setText Java代码中删除电子邮件地址,然后运行该应用程序。电话号码变成蓝色并带有下划线,好像可以单击一样,但是当我点击它时,什么也没有发生。
我该如何工作?
此外,我在清单文件以及仿真器设备上都授予了CALL_PHONE权限。
答案 0 :(得分:1)
From the email (.co.uk), I suppose that you are from UK. You just need to add your country phone number prefix to replace 0, which is +44. And you don't need to use Html.fromHtml
.
careersGuidance.setText("Help with choosing the right course and with thinking about where this might take you in the future.\n\nTel: +441274433043\n\nEmail: careers@bradfordcollege.ac.uk");
In your xml, you just need this property
android:autoLink="all"
答案 1 :(得分:0)
您必须将国家/地区代码添加到电话号码中。 像这样:
careers_guidance_text.setText(Html.fromHtml("
<p>Help with choosing the right course and with thinking about where this might take you in the future.</p>"
+"<p>Tel: <a href=\"tel:+4401274433043\">+441274 433043</a></p>
Email: <a href=\"mailto:careers@bradfordcollege.ac.uk\">
careers@bradfordcollege.ac.uk</a>"));
还必须在android清单文件中添加CALL PHONE权限。
uses-permission android:name="android.permission.CALL_PHONE"
。