技术控

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

[其他] BetterLinkMovementMethod

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

BetterLinkMovementMethod

BetterLinkMovementMethod

   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 :
  1. repositories {
  2.     jcenter()
  3. }
  4. dependencies {
  5.     compile 'me.saket:better-link-movement-method:1.0'
  6. }
复制代码
Usage

  BetterLinkMovementMethod can be used in the same way as you’d use a normal LinkMovementMethod.
  1. TextView textView = (TextView) findViewById(R.id.text1);
  2. textView.setMovementMethod(BetterLinkMovementMethod.newInstance());
  3. Linkify.addLinks(textView, Linkify.PHONE_NUMBERS);
复制代码
However, the easiest way to get started is by using one of its linkify() methods:
  1. BetterLinkMovementMethod.linkify(int linkifyMask, Activity);
  2. BetterLinkMovementMethod.linkify(int linkifyMask, ViewGroup);
  3. BetterLinkMovementMethod.linkify(int linkifyMask, TextView...);
  4. // Where linkifyMask can be one of Linkify.ALL, Linkify.PHONE_NUMBERS,
  5. // Linkify.MAP_ADDRESSES, Linkify.WEB_URLS and Linkify.EMAIL_ADDRESSES.
复制代码
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:
  1. BetterLinkMovementMethod method = BetterLinkMovementMethod.linkify(Linkify.ALL, this);
  2. method.setOnLinkClickListener((textView, url) -> {
  3.     // Do something with the URL and return true to indicate that this URL was handled.
  4.     // Otherwise, return false to let the framework handle the URL.
  5.     return true;
  6. });
  7. // Or the less verbose way
  8. BetterLinkMovementMethod
  9.         .linkify(Linkify.ALL, this)
  10.         .setOnLinkClickListener((textView, url) -> {
  11.             // Do something.
  12.             return true;
  13.         });
复制代码
You can also choose to go the shorter route of registering BetterLinkMovementMethod on all TextViews in your Activity’s layout in one go:
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(R.layout.activity_main);
  5.     BetterLinkMovementMethod.linkify(Linkify.ALL, this);
  6. }
复制代码
When using in a non-Activity context (e.g., Fragments), you can also pass a ViewGroup as the 2nd param:
  1. @Nullable
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  4.     View view = inflater.inflate(R.layout.your_fragment, container, false);
  5.     BetterLinkMovementMethod.linkify(Linkify.ALL, ((ViewGroup) view));
  6.     return view;
  7. }
复制代码
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
邓胜华 发表于 2016-10-22 15:25:37
很好很强大。。
回复 支持 反对

使用道具 举报

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

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表