在android中的textview中单击时获取链接文本的值

发布于 2024-12-09 13:52:15 字数 1691 浏览 0 评论 0原文

我有一个TextView。我通过匹配一些正则表达式模式添加了自定义链接,例如 "@abc""#android"。链接显示正确。但是我没有办法提取所单击的链接的文本。我正在使用 SpannableString 将 Text 设置为文本视图。然后,我使用自定义的 ClickableSpan 设置跨度。效果很好。另外我还可以捕获 onclick 事件。但是 onClick() 方法有一个 View 参数。如果我在视图上调用 getText() (当然是在将其类型转换为 TextView 之后),它会返回整个文本。 我搜索了很多,但总是找到添加链接和捕捉事件的方法,但没有人告诉我如何获取链接的文本。

这是我用来添加链接并接收 onclick 的代码。我从 SO 线程之一获得了代码。

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:

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

是否可以获取单击的链接的文本? 如果没有,是否有一种方法可以将某些数据与特定链接相关联并知道哪个链接被点击? 任何指点。

谢谢

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.

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

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉墨 2024-12-16 13:52:15

解决方案是这样的 -

使用文本视图和要添加的文本调用 setLinks() 。

setLinks(textView, text);

setLinks() 函数是 -

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 类是这样的 -

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

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

    }

handleLinkClicked() 是 -

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

The solution goes like this -

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

setLinks(textView, text);

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

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() 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
    }
}
夏日浅笑〃 2024-12-16 13:52:15

这是我发现的一个非常简单的解决方案,当用户单击它时,它可以获取 TextView 内链接的值。在本例中,我使用的是电话号码,它的作用就像一个魅力。

myTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(myTextView.getSelectionStart()== -1 && 
                    myTextView.getSelectionEnd() == -1){
                Toast.makeText(getApplicationContext(), "You clicked outside the link", 
                        Toast.LENGTH_SHORT).show();
            }
            else {

                int start = myTextView.getSelectionStart();
                int end = myTextView.getSelectionEnd();
                String selected = myTextView.getText().toString().substring(start, end);

                Toast.makeText(getApplicationContext(), 
                        "Clicked: " + selected, 
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

希望有帮助。

Here is a pretty simple solution I found to get the value of the link inside the TextView when the user clicks on it. In this case I'm using phone numbers and it works like a charm.

myTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(myTextView.getSelectionStart()== -1 && 
                    myTextView.getSelectionEnd() == -1){
                Toast.makeText(getApplicationContext(), "You clicked outside the link", 
                        Toast.LENGTH_SHORT).show();
            }
            else {

                int start = myTextView.getSelectionStart();
                int end = myTextView.getSelectionEnd();
                String selected = myTextView.getText().toString().substring(start, end);

                Toast.makeText(getApplicationContext(), 
                        "Clicked: " + selected, 
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

Hope it helps.

雨夜星沙 2024-12-16 13:52:15

使用

android:linksClickable="true"
android:autoLink="web"

textView.setMovementMethod(LinkMovementMethod.getInstance())

Use

android:linksClickable="true"
android:autoLink="web"

textView.setMovementMethod(LinkMovementMethod.getInstance())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文