获取链接文本的价值在Android的一个TextView点击时文本、价值、链接、TextView

2023-09-05 07:53:25 作者:﹏没事耍耍尐脾气゛

我有一个的TextView 。我已经添加例如@ ABC风俗通 #机器人通过匹配一些正则表达式。这些链接显示正常。不过我没有得到一个方法来提取该被点击的链接的文本。我使用的 SpannableString 以的s​​etText到TextView的。然后,我用我的自定义的 ClickableSpan 设置跨度。它工作正常。另外,我还可以赶上onclick事件。但在的onClick ()方法有一个视图参数中。如果我称之为的getText()在View(ofcourse它值映射为的TextView后),则返回整个文本。 我搜索了很多,但总是能找到方法来添加链接,赶上活动,但没有告知获取链接的文本。

I have a TextView. I have added custom links like "@abc", "#android" by matching some regex pattern. The links are displaying properly. However I am not getting a way to extract the text of the link which is clicked. I am using SpannableString to setText to the textview. I then set spans using my custom ClickableSpan. It works fine. Plus I can also catch the onclick event. But the onClick() method has a View paramter. If I call getText() on the View (ofcourse after typecasting it to TextView), it returns the entire text. I searched a lot but always found ways to add links and catch the event, but none told about getting the text of the link.

这是在code我使用添加链接,并免费获赠的onclick。我从SO一个线程拿到了code ..

This is the code I am using to add links and recieve onclick. I got the code from one of the SO threads..

Pattern pattern = Pattern.compile("@[\\w]+");
Matcher matcher = pattern.matcher(tv.getText());//tv is my TextView
while (matcher.find()) {
    int x = matcher.start();
    int y = matcher.end();
    final android.text.SpannableString f = new android.text.SpannableString(
    tv.getText());
    f.setSpan(new InternalURLSpan(new View.OnClickListener() {
        public void onClick(View v) {
        showDialog(1);
    }
}), x, y, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(f);
tv.setLinkTextColor(Color.rgb(19, 111, 154));
tv.setLinksClickable(true);

下面是InternalURLSpan:

Here is the InternalURLSpan:

class InternalURLSpan extends android.text.style.ClickableSpan {
    View.OnClickListener mListener;

    public InternalURLSpan(View.OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
        mListener.onClick(widget);
        TextView tv = (TextView) widget;
        System.out.println("tv.gettext() :: " + tv.getText());
        Toast.makeText(MyActivity.this,tv.getText(),
        Toast.LENGTH_SHORT).show();
    }
}

时有可能得到的链接点击的文字? 如果没有,有没有一些数据关联到一个特定的链接,并知道哪些链接被点击的方法吗? 任何指针。

Is it possible to get the text of the link clicked? If not, is there a way of associating some data to a particular link and knowing which link gets clicked? Any pointers.

感谢

推荐答案

解决的办法是这样的 -

The solution goes like this -

呼叫setLinks()与你的TextView和文字添加。

Call setLinks() with you textview and the text to be added.

setLinks(textView, text);

setLinks()函数为 -

setLinks() function is as -

void 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);
    }

和InternalURLSpan类是这样的 -

and the InternalURLSpan class goes like this -

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

        @Override
        public void onClick(View widget) {
            handleLinkClicked(text);
        }

    }

handleLinkClicked()一​​样 -

handleLinkClicked() is as -

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