zoukankan      html  css  js  c++  java
  • Android超链接

    第一种:

    text += "<a href='http://www.baidu.com'>百度超链接</a>";
    CharSequence charSequence = Html.fromHtml(text);
    textview.setText(charSequence);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

    Html.fromHtml为什么返回CharSequence?
    答:TextView是不只可以String的,我们平常用的给setText()方法传递String参数的时候,其实是调用的public final void setText (CharSequence text)方法,String类是CharSequence的子类。
    而CharSequence子类众多,其中有一个接口Spanned,即类似html的带标记的文本。我们可以用它来在TextView中显示html(自然,有很多html标记是不支持的,只支持一部分)。 

    第二种:

    <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="all" />
    textview = (TextView) findViewById(R.id.textview);
    String str = "我的号码:13926190296
    ";
    str += "百度的网址:http://www.baidu.com";
    textview.setText(str);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

    第三种:

    textview = (TextView) findViewById(R.id.textview);
    String text = "百度连接
    ";
    SpannableString str = new SpannableString(text);
    Object obj = new URLSpan("http://www.baidu.com");
    str.setSpan(obj, 0, 4, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    textview.setText(str);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

    第四种(图片做链接): 

    String text = "百度链接";
    SpannableString str = new SpannableString(text);
    Resources resources = getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher);
    Object obj0 = new ImageSpan(MainActivity.this,bitmap);
    Object obj1 = new URLSpan("http://www.baidu.com");
    str.setSpan(obj0, 0, 4, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    str.setSpan(obj1, 0, 4, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    textview.setText(str);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

    注:

    textview.setMovementMethod(LinkMovementMethod.getInstance());

    是必须要的

  • 相关阅读:
    关于silverlight打印模糊的问题
    Microsoft Office Excel 不能访问文件及COM无法访问
    IE8 下 select option 内容过长 , 展开时信息显示不全解决办法
    如何用css做一个细虚线边框表格
    DIV 垂直 垂直水平 居中
    Ul li 横排 菜单
    对原生js的一些小尝试
    Nodejs学习笔记——Assert(断言)
    Octopress创建GitHub Pages——基于代码托管的静态博客
    JS倒计时器一只,顺便复习javascript时间相关函数
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5284926.html
Copyright © 2011-2022 走看看