当前位置: 首页 > 软件库 > 手机/移动开发 > >

Android-TextView-LinkBuilder

授权协议 MIT License
开发语言 Java
所属分类 手机/移动开发
软件类型 开源软件
地区 不详
投 递 者 关冠宇
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Android TextView-LinkBuilder

Insanely easy way to create clickable links within a TextView.

While creating Talon for Twitter, one of the most difficult things I encountered was creating these clickable links based on specific text. Luckily, I have made it easy for anyone to apply this type of style to their TextView's.

Features

Similar to how all the big players do it (Google+, Twitter, cough Talon cough), this library allows you to create clickable links for any combination of Strings within a TextView.

  • Specify long and short click actions of a specific word within your TextView
  • Provide user feedback by highlighting the text when the user touches it
  • Match single Strings or use a regular expression to set clickable links to any text conforming to that pattern
  • Change the color of the linked text
  • Change the color of the linked text when the user touches it
  • Modify the transparency of the text's highlighting when the user touches it
  • Set whether or not you want the text underlined
  • Set whether or not you want the text bold
  • Default link color from an activity theme

The main advantage to using this library over TextView's autolink functionality is that you can link anything, not just web address, emails, and phone numbers. It also provides color customization and touch feedback.

Installation

There are two ways to use this library:

As a Gradle dependency

This is the preferred way. Simply add:

dependencies {
    compile 'com.klinkerapps:link_builder:2.0.5'
}

to your project dependencies and run gradle build or gradle assemble.

As a library project

Download the source code and import it as a library project in Eclipse. The project is available in the folder library. For more information on how to do this, read here.

Example Usage

Functionality can be found in the Kotlin example's MainActivity. For Java check JavaMainActivity.

For a list of regular expressions that I use in Talon, you can go here

// Create the link rule to set what text should be linked.
// can use a specific string or a regex pattern
Link link = new Link("click here")
    .setTextColor(Color.parseColor("#259B24"))                  // optional, defaults to holo blue
    .setTextColorOfHighlightedLink(Color.parseColor("#0D3D0C")) // optional, defaults to holo blue
    .setHighlightAlpha(.4f)                                     // optional, defaults to .15f
    .setUnderlined(false)                                       // optional, defaults to true
    .setBold(true)                                              // optional, defaults to false
    .setOnLongClickListener(new Link.OnLongClickListener() {
        @Override
        public void onLongClick(String clickedText) {
        	// long clicked
        }
    })
    .setOnClickListener(new Link.OnClickListener() {
        @Override
        public void onClick(String clickedText) {
        	// single clicked
        }
    });

TextView demoText = (TextView) findViewById(R.id.test_text);

// create the link builder object add the link rule
LinkBuilder.on(demoText)
    .addLink(link)
    .build(); // create the clickable links

You can also create a CharSequence instead of creating and applying the links directly to the TextView. Do not forget to set the movement method on your TextView's after you have applied the CharSequence, or else the links will not be clickable.

// find the text view. Used to create the link builder
TextView demoText = (TextView) findViewById(R.id.test_text);

// Add the links and make the links clickable
CharSequence sequence = LinkBuilder.from(this, demoText.getText().toString())
    .addLinks(getExampleLinks())
    .build();

demoText.setText(sequence);

// if you forget to set the movement method, then your text will not be clickable!
demoText.setMovementMethod(TouchableMovementMethod.getInstance());

If you would like to set the default text color for links without inputting it manually on each Link object, it can be set from the activity theme.

<style name="LinkBuilderExampleTheme" parent="android:Theme.Holo.Light">
    <item name="linkBuilderStyle">@style/LinkBuilder</item>
</style>
<style name="LinkBuilder">
    <item name="defaultLinkColor">#222222</item>
    <item name="defaultTextColorOfHighlightedLink">#444444</item>
</style>

Kotlin Support

The library is built on Kotlin, so you get some extension methods that you can use to apply the links to the TextView, instead of creating the builder.

val demo = findViewById<TextView>(R.id.demo_text)
demo.applyLinks(link1, link2, ...)
demo.applyLinks(listOfLinks)

Usage with ListView.OnItemClickListener

By default, LinkBuilder will consume all the touch events on your TextView. This means that ListView.OnItemClickListener will never get called if you try to implement it. The fix for this is to implement the LinkConsumableTextView rather than the normal TextView in your layouts.

My LinkConsumableTextView will only consume touch events if you have clicked the link within the TextView. Otherwise, it will defer the touch event to the parent, which allows you to use ListView.OnItemClickListener method.

Contributing

Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.

Changelog

The full changelog for the library can be found here.

License

Copyright 2015 Luke Klinker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 相关资料
  • 问题内容: 所以我想补充superscipt /标到一个TextView,基本上是试图使一小部分显示为1 / 2。不知道我要去哪里错,而且我是Android的新手,因此感谢您的帮助。谢谢! 问题答案: 试试吧。简单而精确: 注意:您可能需要给文本视图一些高度,否则输出将被剪切。 或者,您可以尝试使用此方法,其中数字大小较小:

  • 我想要一个TextView应该分成4行。对于例如。 注意,重力应为 我试着如下: 这管用!但会产生以下输出: 问题: 对于单词Vishal和vyas之间的空格不起作用。 是harcoded的,可以有任何字符数n的名称,而不是。 请指教。 补充说:如果我需要编写一个自定义的来实现这一点,这是很好的,但我需要一些指导。 提前道谢。

  • 前面 6 节分别学习了 Android 的六大布局,它的功能就是将 View 及 ViewGroup 按照一定的规则摆放起来,那么接下来的章节我们就来学习 Android 中常见的 View。 首先我们看看 TextView,它是 Android 中很常用的 View,用来展示文本信息。它通常会其他的 View 一起搭配使用,用来作为提示信息。比如我们前面做过的登陆界面,就会搭配 EditText

  • 问题内容: 我想双击我已经在下面的代码使用的textview 但它仍然不起作用:( 仅拖动正在调用,但从未调用过“双击”和“一键单击” 问题答案: 请尝试以下步骤。 第1步 在您的活动中编写以下代码。 第2步 为活动编写以下代码。这将是对象。

  • 问题内容: 我在Android应用程序中设置了Twitter数据。我想使用Spannable使字体变为粗体和斜体,但它不起作用,出现错误。我该怎么做? 问题答案: 我想将字体设为粗体和taltal 为此,您将需要制作文本,然后将其设置为TextView: 编辑: 您还可以使用Html.fromHtml在textview中使文本为粗体和斜体为:

  • 本文向大家介绍Android TextView预渲染研究,包括了Android TextView预渲染研究的使用技巧和注意事项,需要的朋友参考一下 Android中的TextView是整个framework中最复杂的控件之一,负责Android中显示文本的大部分工作,framwork中的许多控件也直接或者间接的继承于TextView,例如Button,EditText等。其内部实现也相当复杂,单论