android 在TextView中处理多个ClickableSpan

wkyowqbh  于 2023-01-19  发布在  Android
关注(0)|答案(4)|浏览(178)

我已经在这个问题上纠结了很久了。我得到的是一个简单的字符串"This is a link and this is another link"。我想让两个“链接”单词都可以点击,在浏览器中打开不同的URL。

  • 我能做的最简单的方法是在两个带有不同URL的“link”单词上设置Click-able Span,但我面临的问题是找到Span的开始和结束位置。文本是动态的,我必须通过编程找到位置。
  • 一种方法是找到单词“link”的第一次出现,找到开始和结束位置并设置跨度,然后是第二次出现。但这并不可靠。文本可能包含不止一种重复的单词,如"This is a cat link and this is another cat link"。这里我必须通过可点击跨度将“cat”和“link”单词链接到不同的URL。我该如何操作?
628mspwn

628mspwn1#

用这种方式试试

String s="Cat link 1 Cat link 2 Cat link 3";
    SpannableString ss = new SpannableString(s);
    String first ="Cat link 1";
    String second ="Cat link 2";
    String third ="Cat link 3";
    int firstIndex = s.toString().indexOf(first);
    int secondIndex = s.toString().indexOf(second);
    ClickableSpan firstwordClick = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ///............
        }
    }; 
    ClickableSpan secondwordClick = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ///............
        }
    }; 
    ss.setSpan(firstwordClick,firstIndex, firstIndex+first.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(secondwordClick,secondIndex, secondIndex+second.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setLinksClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(ss,BufferType.SPANNABLE);
pw9qyyiw

pw9qyyiw2#

如果您不能通过编程找到链接中的差异,那么您就不能期望其他任何东西能够做到这一点。作为开发人员,您需要一个系统。
你需要能够识别可点击的范围--因为链接是唯一的,所以标识它们的文本也必须是唯一的,这对你的用户来说很可能是个问题。
你可以得到一个有序的URL列表,如果链接无法区分,只需按照你收到的顺序使用它们。或者,你需要更改创建链接的规则或它们的显示顺序。

n1bvdmb6

n1bvdmb63#

一个简单的方法是在链接前加上一个标识符,比如/*/,然后用这个标识符找到链接的起始和结束位置。找到后,先用""替换标识符,然后点击离开。

jgwigjjp

jgwigjjp4#

我有这样的字符串**“您订单ID为{b1 j2 gh4 b}的订单已被电话号码为(1234124124)的bla bla sotre认领"**
我使用这些大括号是为了找出orderIDPhone number的索引

String notificationMessage = mArrListNotification.get(position).getMessage();
            boolean isPhoneNumberAvailable = false, isOrderIdAvailable = false;
            int phoneStartIndex = 0, phoneEndIndex = 0, orderIdStartIndex = 0, orderIdEndIndex = 0;

    //getting index on the basis of braces added to text
            if (notificationMessage.contains("(")) {
                isPhoneNumberAvailable = true;
                phoneStartIndex = notificationMessage.indexOf("(");
                phoneEndIndex = notificationMessage.indexOf(")");
            }
            if (notificationMessage.contains("{")) {

                orderIdStartIndex = notificationMessage.indexOf("{");
                orderIdEndIndex = notificationMessage.indexOf("}");

            }

            // we got the index so remove braces
            notificationMessage = notificationMessage.replace("(", " ");
            notificationMessage = notificationMessage.replace(")", " ");
            notificationMessage = notificationMessage.replace("{", " ");
            notificationMessage = notificationMessage.replace("}", " ");

            viewHolder.txtNotificationMessage.setText(notificationMessage, TextView.BufferType.SPANNABLE);
            Spannable mySpannablePhoneNumber = (Spannable) viewHolder.txtNotificationMessage.getText();
            Spannable mySpannableOrderID = (Spannable) viewHolder.txtNotificationMessage.getText();
            ClickableSpan mySpanPhoneClick = new ClickableSpan() {
                @Override
                public void onClick(View widget) {

                    currentPosition = (Integer) widget.getTag();

                    String message = mArrListNotification.get(currentPosition).getMessage();
                    int startIndex = message.indexOf("(");
                    int endIndex = message.indexOf(")");
                    phoneNumber = message.substring(startIndex + 1, endIndex);

     Log.i("Phone Number", phoneNumber clicked)

                }
            };
            ClickableSpan mySpanOrderClick = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    currentPosition = (Integer) widget.getTag();

                    String message = mArrListNotification.get(currentPosition).getMessage();
                    int startIndex = message.indexOf("{");
                    int endIndex = message.indexOf("}");
                    String orderID = message.substring(startIndex + 1, endIndex);
    // Log.i("Order id", orderID  clicked)

                }
            };
            if (isPhoneNumberAvailable) {
                mySpannablePhoneNumber.setSpan(mySpanPhoneClick, phoneStartIndex + 1, phoneEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (isOrderIdAvailable) {
                mySpannableOrderID.setSpan(mySpanOrderClick, orderIdStartIndex + 1, orderIdEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

相关问题