技术控

    今日:89| 主题:49113
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] BetterLinkMovementMethod

[复制链接]
雨后云初霁 发表于 2016-10-5 11:28:09
74 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

BetterLinkMovementMethod-1 (Android,android,detailed,Twitter,numbers)

   When android:autoLink="all" or Linkify.addLinks(textView, Linkify.ALL) is used to add links to web URLs, phone-numbers, map addresses or email addresses in a TextView, Android uses a class known as LinkMovementMethod that handles highlighting links when they're focused and dispatching an Intent when they're clicked.
   BetterLinkMovementMethod improves over LinkMovementMethod , by fixing its flaws:
  
       
  • No support for custom URL click listeners. For eg., phone numbers always show up in the dialer when clicked and there's no way to manually handle the click.   
  • Incorrect calculation of touch areas for links, resulting in ghost touch areas ( Example video )   
  • Unreliable highlighting of links ( Example video )  
   A detailed explanation of why (and when) you should use BetterLinkMovementMethod can be read on my blog: http://saket.me/better-url-handler-textview-android/
   Feel free to give a shoutout on Twitter @Saketme if you're using this in your app.
  Download

   Add this to your module's build.gradle :
  [code]repositories {
    jcenter()
}

dependencies {
    compile 'me.saket:better-link-movement-method:1.0'
}[/code]  Usage

  BetterLinkMovementMethod can be used in the same way as you’d use a normal LinkMovementMethod.
  [code]TextView textView = (TextView) findViewById(R.id.text1);
textView.setMovementMethod(BetterLinkMovementMethod.newInstance());
Linkify.addLinks(textView, Linkify.PHONE_NUMBERS);[/code]  However, the easiest way to get started is by using one of its linkify() methods:
  [code]BetterLinkMovementMethod.linkify(int linkifyMask, Activity);
BetterLinkMovementMethod.linkify(int linkifyMask, ViewGroup);
BetterLinkMovementMethod.linkify(int linkifyMask, TextView...);

// Where linkifyMask can be one of Linkify.ALL, Linkify.PHONE_NUMBERS,
// Linkify.MAP_ADDRESSES, Linkify.WEB_URLS and Linkify.EMAIL_ADDRESSES.[/code]  Examples

  Registering a BetterLinkMovementMethod on a TextView:
  BetterLinkMovementMethod.linkify(Linkify.ALL, textView);
  or on infinite TextViews:
  BetterLinkMovementMethod.linkify(Linkify.ALL, textView1, textView2, textView3, ...);
  Adding a click listener:
  [code]BetterLinkMovementMethod method = BetterLinkMovementMethod.linkify(Linkify.ALL, this);
method.setOnLinkClickListener((textView, url) -> {
    // Do something with the URL and return true to indicate that this URL was handled.
    // Otherwise, return false to let the framework handle the URL.
    return true;
});

// Or the less verbose way
BetterLinkMovementMethod
        .linkify(Linkify.ALL, this)
        .setOnLinkClickListener((textView, url) -> {
            // Do something.
            return true;
        });[/code]  You can also choose to go the shorter route of registering BetterLinkMovementMethod on all TextViews in your Activity’s layout in one go:
  [code]@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BetterLinkMovementMethod.linkify(Linkify.ALL, this);
}[/code]  When using in a non-Activity context (e.g., Fragments), you can also pass a ViewGroup as the 2nd param:
  [code]@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_fragment, container, false);

    BetterLinkMovementMethod.linkify(Linkify.ALL, ((ViewGroup) view));

    return view;
}[/code]  Contributions

  If you think that the APIs or the implementation can be improved, please feel free to raise a pull-request.
  License

   This library is licensed under Apache 2.0 which can be read here
友荐云推荐




上一篇:运维之殇
下一篇:Sourcegraph provides intelligent searches of Go language code
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

邓胜华 发表于 2016-10-22 15:25:37
很好很强大。。
回复 支持 反对

使用道具 举报

蕾冰 发表于 2016-11-1 11:40:02
有钱,就是任性,没钱,认命!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表