Linkify介绍

Linkify是一个辅助类,通过RegEx样式匹配,自动地在TextView类(和继承的类)中创建超链接。

 

符合特定的RegEx样式的文本会被转变成可点击的超链接,这些超链接隐式地调用startActivity(new Intent(Intent.ACTION_VIEW, uri)),符合的文本会作为目标URI

 

你可以指定任意的字符串样式为链接;方便地,Linkify类提供了预置的通用内容类型(如电话号码和e-mailweb地址)。

 

本地的链接类型

 

Linkify.addLinks静态方法接受一个View来制作链接,还包括一个或多个支持的默认内容类型的位结果。Linkify类提供了一些内容类型:WEB_URLS、EMAIL_ADDRESSES、PHONE_NUMBERS和ALL.

 

接下来的代码片段显示如何为TextView制作链接显示webe-mail地址为超链接。当点击时,它们会相应地打开浏览器或e-mail应用程序。

 

TextView textView = (TextView)findViewById(R.id.myTextView);

Linkify.addLinks(textView, Linkify.WEB_URLS|Linkify.EMAIL_ADDRESSES);

 

 

你可以在layout资源里使用android:autoLink特性来为View制作链接。它支持一个或多个(用|分割)自定义的值:nonewebemailphoneall。接下来的XML片段显示了如何为电话号码和e-mail地址添加超链接:

 

<TextView

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:text=”@string/linkify_me”

android:autoLink=”phone|email”

/>

 

 

创建自定义的链接字符串

 

为了定义自己的链接字符串,你需要创建一个RegEx样式来匹配文本,进而显示成超链接。

 

和本地类型一样,通过调用Linkify.addLinks来指定目标View,但这次,传入的是新的RegEx样式。你还可以传入一个前缀,当链接点击时,它会添加到目标URI上。

 

接下来的例子显示了一个View链接到由Android Content Provider(下一章你会创建)提供的地震数据。与包含所有的情况相比,链接样式能匹配任何以“quake”开头后跟一个数字的文本。在Intent被触发前,内容会被添加到URI上。

 

int flags = Pattern.CASE_INSENSITIVE;

Pattern p = Pattern.compile(“\\bquake[0-9]*\\b”, flags);

Linkify.addLinks(myTextView, p, “content://com.paad.earthquake/earthquakes/”);

  

Linkify还支持TranformFilterMatchFilter接口。它们提供一些对目标URI的额外控制和定义匹配字符串,它们的使用如下的框架代码所示:

 

Linkify.addLinks(myTextView, pattern, prefixWith, new MyMatchFilter(), new MyTransformFilter());

  

使用Match Filter

 

在你定义的MatchFilter中实现acceptMatch方法,来为RegEx样式匹配添加额外的条件。当一个潜在的匹配发现时,acceptMatch被触发,匹配的开始点和结束点(包括被查找的整个文本)以参数的形式传入。

 

接下来的代码显示了一个MatchFilter的实现,它取消任何之前是一个“!”的匹配。

 

class MyMatchFilter implements MatchFilter {

public boolean acceptMatch(CharSequence s, int start, int end) {

return (start == 0 || s.charAt(start-1) != ‘!’);

}

}

  

使用Transform Filter

 

Transform Filter为格式化文本字符串提供了更大的自由度,允许你修改由链接文本自动生成的隐式URI。减少链接文本和目标URI的耦合能更加自由地决定如何显示数据字符串给用户。

 

使用Transform Filter,在你定义的TransformFilter中实现transformUrl方法。当Linkify找到正确的匹配后,它会调用transformUrl,传入使用的RegEx样式和它创建的默认URI字符串。你可以修改匹配的字符串,然后返回一个适合给其它Android应用程序“看”的URI

 

下面的TransformFilter实现将匹配的文本转换成小写的URI

 

class MyTransformFilter implements TransformFilter {

public String transformUrl(Matcher match, String url) {

return url.toLowerCase();

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. spyder 5.4.1 requires pyqt5<5.16, which is not installed. spyder 5.4.1 requires pyqtwebengine<5.16, which is not installed. Successfully installed aiofiles-23.1.0 altair-4.2.2 blinker-1.6.2 cachetools-5.3.1 chardet-5.1.0 cmake-3.26.3 cpm_kernels-1.0.11 fastapi-0.95.2 ffmpy-0.3.0 gitdb-4.0.10 gitpython-3.1.31 gradio-3.32.0 gradio-client-0.2.5 h11-0.14.0 httpcore-0.17.2 httpx-0.24.1 latex2mathml-3.76.0 linkify-it-py-2.0.2 lit-16.0.5 markdown-it-py-2.2.0 mdit-py-plugins-0.3.3 mdtex2html-1.2.0 mdurl-0.1.2 nvidia-cublas-cu11-11.10.3.66 nvidia-cuda-cupti-cu11-11.7.101 nvidia-cuda-nvrtc-cu11-11.7.99 nvidia-cuda-runtime-cu11-11.7.99 nvidia-cudnn-cu11-8.5.0.96 nvidia-cufft-cu11-10.9.0.58 nvidia-curand-cu11-10.2.10.91 nvidia-cusolver-cu11-11.4.0.1 nvidia-cusparse-cu11-11.7.4.91 nvidia-nccl-cu11-2.14.3 nvidia-nvtx-cu11-11.7.91 orjson-3.8.14 protobuf-3.20.3 pydantic-1.10.8 pydeck-0.8.1b0 pydub-0.25.1 pygments-2.15.1 pympler-1.0.1 python-multipart-0.0.6 rich-13.4.1 semantic-version-2.10.0 sentencepiece-0.1.99 smmap-5.0.0 starlette-0.27.0 streamlit-1.22.0 streamlit-chat-0.0.2.2 torch-2.0.1 transformers-4.27.1 triton-2.0.0 tzlocal-5.0.1 uc-micro-py-1.0.2 uvicorn-0.22.0 validators-0.20.0 websockets-11.0.3 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv 解释下
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值