textview设置属性autolink后,点击网址链接如何捕捉到点击事件或者如何让其跳转到特定的app activity中

autoLink 一共有六个值 :web  phone  map  email  all  none  分别是url链接  电话号码提取拨号  地图地址  电子邮件  all是能够支持超链接的全部都起作用   最后一个就是都不起作用

  1. 使用方法

    id="@+id/text_view"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:autoLink="web"  
        android:text="@string/hello" />  

url的点击事件

public class MainActivity extends Activity {

  
    TextView tv;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        tv = (TextView) findViewById(R.id.tv);  
        CharSequence text = tv.getText();  
        if (text instanceof Spannable) {  
            int end = text.length();  
            Spannable sp = (Spannable) text;  
            URLSpan urls[] = sp.getSpans(0, end, URLSpan.class);  
            SpannableStringBuilder style = new SpannableStringBuilder(text);  
            style.clearSpans();  
            for (URLSpan urlSpan : urls) {  
                MyURLSpan myURLSpan = new MyURLSpan(urlSpan.getURL());  
                style.setSpan(myURLSpan, sp.getSpanStart(urlSpan),  
                        sp.getSpanEnd(urlSpan),  
                        Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  
  
            }  
            tv.setText(style);  
        }  
    }  
  
    private class MyURLSpan extends ClickableSpan {  
  
        private String url;  
  
        public MyURLSpan(String url) {  
            this.url = url;  
        }  
  
        @Override  
        public void onClick(View arg0) {  
            startActivity(new Intent(WXEntryActivity.this,RegisterActivity.class));  
        }  
  
    }  
  
}  

3.上面两步完美的解决了网站链接标高亮显示并且有下划线,点击时,可以在跳转到确定的Activity中




你可能感兴趣的:(Android进阶)