Linkify调用java代码
有没有办法让 Linkify
链接调用我的 Activity
中的方法,或者我只允许使用 Linkify
创建链接其他活动/应用程序等?
Is there a way to have a Linkify
'd link call a method in my Activity
or am I only allowed to use Linkify
to create links to other activities / apps etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也可以放弃使用 linkify,而只使用 linkify 的正则表达式将可点击的跨度附加到您想要的位置。这些可点击的跨度可以调用您的代码。
下面是一个示例:Android:从可点击文本启动活动
YOu could also just abandon using linkify and just use the regex of linkify to go and attach clickable spans to the places you want. Those clickable spans can call into your code.
Here's an example: Android: Launch activity from clickable text
听起来你已经解决了其中的几个问题。缺少的部分似乎是在前台有一个现有的 Activity 接收 Intent。您可以通过在清单中将 Activity 声明为 singleTop 来实现此目的 (
android:launchMode="singleTop"
)。因此工作流程如下所示:
1) 通过 addLinks 与正则表达式、Linkify.Matcher 和 Linkify.TransformFilter 链接文本
2) 设置接收器以捕获由单击的链接启动的 Intent。
3)在关联的BroadcastReceiver的onReceive中,设置一个Intent来封装您的链接已被点击的事实。然后在 Activity(已经在前台)上调用 startActivity,传递 Intent。
4) 在 Activity 的 onResume 回调中,检查 Intent 以查看是否应该调用该函数。
It sounds like you've got several pieces of this worked out. The missing piece seems to be having an existing Activity which is in the foreground receive the Intent. You can do that by declaring the Activity to be singleTop in the Manifest (
android:launchMode="singleTop"
).So the workflow looks like:
1) Linkify the text by addLinks with your regex, Linkify.Matcher, and Linkify.TransformFilter
2) Set up a receiver to catch the Intent launched by the clicked link.
3) In the onReceive of the associated BroadcastReceiver, set up an Intent to encapsulate the fact that your link has been clicked. Then call startActivity on the Activity (which is already in the foreground), passing the Intent.
4) In the onResume callback on the Activity, check the Intent to see if the function should be called.
您可以设置一个接收器来侦听特定方案,当您单击链接时,会调用以该方案开头的某个 url。
检查此答案以了解更多详细信息。
You can set a receiver which will listen for a particular scheme and when you click on the link call some url starting with that scheme.
Check this answer for more details.
没有。 Linkify 附带:
EMAIL_ADDRESSES
MAP_ADDRESSES
PHONE_NUMBERS
WEB_URLS
您可以尝试扩展它。 这是 src 代码。
Nope. Linkify comes with:
EMAIL_ADDRESSES
MAP_ADDRESSES
PHONE_NUMBERS
WEB_URLS
You can try extending it. Here's the src code.