用view的XML属性实现超链接

Android 应用的 layout(UI 布局)除了直接改写程序代码的方式外(动态布局),也能使用 XML 文件來做描述(XML-based Layout)。


以下是一个实现超链接(hyperlink)并通过点击自动调用浏览器浏览网页的例子:

该功能的实现非常简单,我们只需要改写两行XML属性而已。

每一个View都有许多属性,我们可以通过XML来描述每一个View的属性,从而达到控制应用程序的效果。下面以TextView为例,有一个android:autoLink属性可以实现超链接:

android:autoLink

Controls whether links such as urls and email addresses are automatically found and converted to clickable links. The default value is "none", disabling this feature.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
none0x00Match no patterns (default).
web0x01Match Web URLs.
email0x02Match email addresses.
phone0x04Match phone numbers.
map0x08Match map addresses.
all0x0fMatch all patterns (equivalent to web|email|phone|map).

This corresponds to the global attribute resource symbol autoLink.


http://code.google.com/intl/zh-TW/android/reference/android/widget/TextView.html#attr_android:autoLink


具体实现:

建立一个android工程,打开main.XML文件,修改如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Young's Blog - http://blog.csdn.net/imyang2007?viewmode=contents" android:autoLink="web" /> </LinearLayout>

我们给TextView对象新增一个android:autoLink属性,并把属性设动为web,这时只要text属性出现URL,textiew就会自动将URL文本转换成可点击的link。程序执行时,只要点击link,就会自动启用浏览器,并连接该网址,效果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值