Android: Linkify TextView

只谈情不闲聊 提交于 2019-11-26 04:27:56

问题


I have textview which I need to linkify. Here\'s what I am doing..

TextView text = (TextView) view.findViewById(R.id.name);
text.setText(\"Android...Update from Android\");
Pattern pattern = Pattern.compile(\"Android\");
String scheme = \"www.android.com\";
Linkify.addLinks(text, pattern, scheme);

The text-view is displayed correctly with the text \"Android...Update from Android\", but I am facing two problems.

1) My text string has two instances of the string \"Android\". So, both the text are linkified. I want only the first occurrence to be linkified. How should I go about it?

2) When I click the linkfy text, it opens the browser, but the URL is weird. The url it tries to open is \"www.comandroid\". I don\'t know whats going wrong here. The text \"android\" in the URL is being replaced. Am I doing something wrong when Linknifying the text.

Any help will be highly appreciated.


回答1:


The easy way is put autoLink in XML layout.

  android:autoLink="web" 



回答2:


I created a static method that does this simply:

/**
 * Add a link to the TextView which is given.
 * 
 * @param textView the field containing the text
 * @param patternToMatch a regex pattern to put a link around
 * @param link the link to add
 */
public static void addLink(TextView textView, String patternToMatch,
        final String link) {
    Linkify.TransformFilter filter = new Linkify.TransformFilter() {
        @Override public String transformUrl(Matcher match, String url) {
            return link;
        }
    };
    Linkify.addLinks(textView, Pattern.compile(patternToMatch), null, null,
            filter);
}

And the OP could use it simply:

addLink(text, "^Android", "http://www.android.com");



回答3:


I don't have much experience with Linkify. So far all I have wanted to do is make urls in the text into links which is handled automatically by a different version of addLinks.

However the regular expression you are using to match on "Android" can be changed to only match the one that starts the string by tweaking your regular expression:

Pattern pattern = Pattern.compile("^Android");

Notice the addition of '^'. This tells the regular expression matcher to only match the pattern 'Android' when it starts the string. If that will work with your strings then great. If you have other cases were the word you want to link is not at the beginning of the line you will need to explore regular expressions some more. I recommend this site to learn more regular-expressions.info




回答4:


If you want to open all the urls which contains in text then you can do this..

            TextView textview=(TextView)findViewById(R.id.text);

    textview.setText("Android....Update from android...www.android.com");
    Linkify.addLinks(textview, Linkify.WEB_URLS);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

It may work for all urls.




回答5:


Here from Shawns answer and SpunkerBaba's comment I've come out with this helper class. This stops the issue of the double appending of what you are changing being added to your url.

package com.your.package;

public class Linkify {

    /**
     * @param textView
     *            textView who's text you want to change
     * @param linkThis
     *            a regex of what text to turn into a link
     * @param toThis
     *            the url you want to send them to
     */
    public static void addLinks(TextView textView, String linkThis, String toThis) {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

}

Useage:

 Linkify.addLinks(profileTextView, "BlundellApps.com", "http://www.BlundellApps.com");



回答6:


Problem solved. Solution to the first problem is the answer provided by Ian Leslie. Solution to the second problem is as follows.The behavior of API "Linkify.addLinks" is such that, it appends the string that you want to linkify to the end of the url. For ex..if you want to linkify text "Android" with "www.android.com". The final URL is "www.android.comAndroid" which is not what I needed.So i used

public static final void addLinks (TextView text, Pattern p, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
TransformFilter transformFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) { 
return ""; 
} }; 

The api transformFilter return a null string. So my final url is "www.android.com"




回答7:


I think what you really need is something as simple as:

final TextView textView = ((BodyViewHolder) holder).textView;
textView.setText(text);
Linkify.addLinks(textView, Linkify.WEB_URLS); // I would do this only during view creation, btw.

You're tracking an URL specifically. Some people suggest using Linkigy.ALL, but you don't want this:

public static final int ALL = WEB_URLS | EMAIL_ADDRESSES | PHONE_NUMBERS | MAP_ADDRESSES;

Also, other people are suggesting adding:

textview.setMovementMethod(LinkMovementMethod.getInstance());

after adding the link rules, but addLinks already does that in its insides ;-)

Cheers!




回答8:


For Koltin with lambda expression, it would be like this

val pattern = Pattern.compile(link.title)
Linkify.addLinks(view, pattern, null, null, { matcher, url -> link.href })



回答9:


TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);


来源:https://stackoverflow.com/questions/4746293/android-linkify-textview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!