处理TextView的链接点击我的Andr​​oid应用程序 [英] handle textview link click in my android app

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

问题描述

我目前在像这样一个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价值的东西的协议,如对myApp://,然后注册的东西,它可以让我的应用程序处理该协议。如果这确实是最好的方法,我想知道如何做到这一点,但我希望有一个更简单的方法,只是说,当一个链接被点击的这个TextView的,我想提出接收事件该链接的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。名称

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.

因此​​,所有我需要做的就是建立一个包含我想传达的信息的网址:

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();

在我的例子,我可以简单地检查数据为空值,因为当以往任何时候都没有空,我就知道它是通过这样的方式被调用链接。从那里,我提取我从网址需要能够显示相应的数据的指示。

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.

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

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