在我的 android 应用程序中处理 textview 链接单击 [英] handle textview link click in my android app

查看:17
本文介绍了在我的 android 应用程序中处理 textview 链接单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在像这样在 TextView 中呈现 HTML 输入:

I'm currently rendering HTML input in a TextView like so:

tv.setText(Html.fromHtml("<a href='test'>test</a>"));

显示的 HTML 是通过外部资源提供给我的,所以我无法随意更改,但我当然可以对 HTML 进行一些正则表达式篡改,以更改 href 值,例如别的东西.

The HTML being displayed is provided to me via an external resource, so I cannot change things around as I will, but I can, of course, do some regex tampering with the HTML, to change the href value, say, to something else.

我想要的是能够直接从应用程序内处理链接点击,而不是让链接打开浏览器窗口.这完全可以实现吗?我猜可以将 href-value 的协议设置为myApp://"之类的东西,然后注册一些可以让我的应用处理该协议的东西.如果这确实是最好的方法,我想知道它是如何完成的,但我希望有一种更简单的方法可以说,当在这个文本视图中点击一个链接时,我想引发一个接收到的事件链接的href值作为输入参数"

What I want is to be able to handle a link click directly from within the app, rather than having the link open a browser window. Is this achievable at all? I'm guessing it would be possible to set the protocol of the href-value to something like "myApp://", and then register something that would let my app handle that protocol. If this is indeed the best way, I'd like to know how that is done, but I'm hoping there's an easier way to just say, "when a link is clicked in this textview, I want to raise an event that receives the href value of the link as an input parameter"

推荐答案

将近一年后,我用不同的方式解决了我的特定问题.由于我希望链接由我自己的应用程序处理,因此有一个更简单的解决方案.

Coming at this almost a year later, there's a different manner in which I solved my particular problem. Since I wanted the link to be handled by my own app, there is a solution that is a bit simpler.

除了默认的意图过滤器之外,我只是让我的目标活动监听 ACTION_VIEW 意图,特别是那些具有方案 com.package.name

Besides the default intent filter, I simply let my target activity listen to ACTION_VIEW intents, and specifically, those with the scheme com.package.name

<intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="com.package.name" />  
</intent-filter>

这意味着以 com.package.name:// 开头的链接将由我的活动处理.

This means that links starting with com.package.name:// will be handled by my activity.

所以我所要做的就是构建一个包含我想要传达的信息的 URL:

So all I have to do is construct a URL that contains the information I want to convey:

com.package.name://action-to-perform/id-that-might-be-needed/

在我的目标活动中,我可以检索到这个地址:

In my target activity, I can retrieve this address:

Uri data = getIntent().getData();

在我的示例中,我可以简单地检查 data 的空值,因为当它不为空时,我会知道它是通过这样的链接调用的.从那里,我从 url 中提取我需要的指令,以便能够显示适当的数据.

In my example, I could simply check data for null values, because when ever it isn't null, I'll know it was invoked by means of such a link. From there, I extract the instructions I need from the url to be able to display the appropriate data.

这篇关于在我的 android 应用程序中处理 textview 链接单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆