android UI Link

在一些说明性的文字中间,可能要加入电话、邮箱、访问网址等连接。如果直接使用TextView类,这些链接是不能点击的。
如果需要把链接变成直接可以点击的,有以下方法

1. 设置TextView 类的autoLink属性  
2. 设置TextView 类的 setMovementMethod方法
3. 用 SpannableString 类


实例

package demo.link;


import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;


public class TestLinkActivity extends Activity
{


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


TextView tv2 = (TextView) findViewById(R.id.tv2);
tv2.setMovementMethod(LinkMovementMethod.getInstance());


TextView tv3 = (TextView) findViewById(R.id.tv3);
tv3.setText(Html.fromHtml("<b>测试3: <br/></b> 欢迎访问"
+ "<a href=\"http://www.google.com\">google</a> "
+ "或致电 <a href='tel:4155551212'>客服热线</a>"));


tv3.setMovementMethod(LinkMovementMethod.getInstance());


SpannableString ss = new SpannableString(
"text4: Click here to dial the phone.");


ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView tv4 = (TextView) findViewById(R.id.tv4); 
tv4.setText(ss);
tv4.setMovementMethod(LinkMovementMethod.getInstance());

}
}

布局

<?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="测试链接" />
    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/text1"
        android:autoLink="all"
        />
    /
    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/link_text_manual"
      
     />
   
    <TextView
        android:id="@+id/tv3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
    <TextView
        android:id="@+id/tv4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="hello">Hello World, TestLinkActivity!</string>
    <string name="app_name">TestLink</string>
    <string name="text1">
        测试1:\n
        如果有任何疑问,请登录 http://www.google.com.cn 。或致电 010-10000   
    </string>
   
    <string name="link_text_manual"><b>测试2:\n</b>请访问
     <a href="http://www.google.com">google</a> 查询详细信息。
           或致电 <a href="tel:4155551212">客服热线</a>.
    </string>    
        
</resources>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值