Android -Convert大段链接到spanURL

时间:2013-07-31 15:53:57

标签: android url textview spannable

已经尝试过在线搜索。这是我想要做的一个例子:

textview中的文字是:“嘿,你是怎么做的,请查看此链接:http://www.google.com如果您不喜欢该链接,请尝试此链接http://yahoo.com或最后尝试http://tinyurl.com/wp-tinyurl

我想在列表视图中点击所有这些链接。我不想在textview对象上使用android:autoLink =“web”,因为列表项本身是可点击的,这些可能会消耗点击事件或引起混淆。我正在寻找一种方法来扫描文本并收集链接,然后将它们更改为spanURL,这样只有文本变为可点击而不是textview。如果它在这里有任何区别是我现在的listview的行布局中的textview:

 <TextView
        android:id="@+id/tv_user_response"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:autoLink="web"
        android:descendantFocusability="blocksDescendants"
        android:textColor="@color/grey_font"
        android:textSize="14sp" />

但我相信我必须以编程方式处理这个问题。

更新: 感谢这里提供的链接是我最终做的:

public class Linkifier {


public TextView setLinks(TextView tv, String text) {
    String[] linkPatterns = {
            "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
            "#[\\w]+", "@[\\w]+" };
    for (String str : linkPatterns) {
        Pattern pattern = Pattern.compile(str);
        Matcher matcher = pattern.matcher(tv.getText());
        while (matcher.find()) {
            int x = matcher.start();
            int y = matcher.end();
            final android.text.SpannableString f = new android.text.SpannableString(
                    tv.getText());
            InternalURLSpan span = new InternalURLSpan();
            span.text = text.substring(x, y);
            f.setSpan(span, x, y,
                    android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(f);
            // tv.setOnLongClickListener(span.l);

        }
    }
    tv.setLinkTextColor(Color.BLUE);
    tv.setLinksClickable(true);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setFocusable(false);

    return tv;

}

class InternalURLSpan extends android.text.style.ClickableSpan {
    public String text;

    @Override
    public void onClick(View widget) {
        Utils.createToast(widget.getContext(),text);
        handleLinkClicked(widget.getContext(),text);
    }


    public void handleLinkClicked(Context context,String value) {
        if (value.startsWith("http")) {     // handle http links
        } else if (value.startsWith("@")) {
            // handle @links

        } else if (value.startsWith("#")) { // handle #links
            String searchTerm=text.replace("#", "");

            String query = null;
            try {
                query = URLEncoder.encode(text, "utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Consts.URL_TWITTER_SEARCH+query));
            context.startActivity(browserIntent);
        }
    }
}

}

然后你可以创建linkifier类,我这样运行: Linkifier linkifier = new Linkifier(); linkifier.setLinks(myTextView,“我的消息等”);

textview必须包含文本,第二个参数与您要查找的文本相匹配。因此,如果myTextView包含“嘿你是怎么做的,请查看此链接:http://www.google.com如果您不喜欢该链接,请尝试此链接http://yahoo.com或最后尝试http://tinyurl.com/wp-tinyurl”然后第二个参数放置相同串。

0 个答案:

没有答案