我有一个包含电子邮件和电话号码的 TextView 。我想要的效果是,当用户点击电子邮件时,它会打开默认的电子邮件应用程序,当用户点击电话号码时,应用程序会在拨号器中将其拉出。使用以下代码:
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 代码中删除电子邮件地址,然后运行该应用程序。电话号码变成蓝色并带有下划线,就好像它可以点击一样,但是当我点击它时,没有任何反应。
我怎样才能让它工作?
此外,我在我的 list 文件和我的模拟器设备上都授予了 CALL_PHONE 权限。
最佳答案
从电子邮件 (.ac.uk) 来看,我猜你来自英国。您只需要添加您所在国家/地区的电话号码前缀来替换 0,即 +44。而且您不需要使用 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");
在你的xml中,你只需要这个属性
android:autoLink="all"
关于android - 如何在 Android 的 TextView 中使电话号码可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717190/