欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android TextView添加超链接的方法示例

程序员文章站 2024-03-31 17:33:10
本文实例讲述了android textview添加超链接的方法。分享给大家供大家参考,具体如下: public class link extends activi...

本文实例讲述了android textview添加超链接的方法。分享给大家供大家参考,具体如下:

public class link extends activity {
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.link);
    // text1 shows the android:autolink property, which
    // automatically linkifies things like urls and phone numbers
    // found in the text. no java code is needed to make this
    // work.
    // text2 has links specified by putting <a> tags in the string
    // resource. by default these links will appear but not
    // respond to user input. to make them active, you need to
    // call setmovementmethod() on the textview object.
    textview t2 = (textview) findviewbyid(r.id.text2);
    t2.setmovementmethod(linkmovementmethod.getinstance());
    // text3 shows creating text with links from html in the java
    // code, rather than from a string resource. note that for a
    // fixed string, using a (localizable) resource as shown above
    // is usually a better way to go; this example is intended to
    // illustrate how you might display text that came from a
    // dynamic source (eg, the network).
    textview t3 = (textview) findviewbyid(r.id.text3);
    t3.settext(
      html.fromhtml(
        "<b>text3:</b> text with a " +
        "<a href=\"http://www.google.com\">link</a> " +
        "created in the java source code using html."));
    t3.setmovementmethod(linkmovementmethod.getinstance());
    // text4 illustrates constructing a styled string containing a
    // link without using html at all. again, for a fixed string
    // you should probably be using a string resource, not a
    // hardcoded value.
    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 t4 = (textview) findviewbyid(r.id.text4);
    t4.settext(ss);
    t4.setmovementmethod(linkmovementmethod.getinstance());
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。