【问题标题】:make hyperlinks of astring on the basis of '#'在'#'的基础上制作astring的超链接
【发布时间】:2014-11-26 10:11:11
【问题描述】:

我正在制作一个基于 json web 服务的应用程序。我遇到了一些问题,我有一个字符串,我必须拆分它然后使其可点击。让我解决我的问题:

->我有类似的字符串:"#outfit#fashion#color""#outfit" 或 "#outfit真的太棒了。”

->现在我想将子字符串的颜色更改为蓝色:#outfit#fashion#color 等。 .and rest substring 将变黑。

->我也想让它可点击 例如:#outfit 将点击并获取与 #outfit 标签相关的所有数据。与标签概念的工作原理相同。

->我有多种类型的字符串,我希望逻辑在每种情况下都能正常工作:

  • “#outfit#fashion#color”
  • “#outfit”
  • #outfit 真的很棒。”
  • "查看我的新 #dress 品牌 #new #outfit。不是 #cool ?”

提前谢谢。只是想让所有标签都变成蓝色并点击。请用完整的代码描述我,因为我的逻辑不太好..

【问题讨论】:

  • 我已经尝试过简单的拆分方式来根据哈希拆分字符串......但我认为它不会在所有情况下都有效。还想将标签染成蓝色。
  • @userAndroid 在什么情况下溢出不起作用?
  • 不知道..正如我告诉过你的那样,我的逻辑不好..简单的拆分将无法使用 for 循环拆分以检查另一个 # 是否剩余或不工作..我我无法做到这一点..这就是我问的原因
  • 基本上你需要在字符串上使用.spilt函数并使字符串像text.setText(Html.fromHtml("<bThis is normal text </b>" + "<a href=\"http://www.xyz-zyyx.com\">+splitString[0]+</a> "))一样点击

标签: java android string logic


【解决方案1】:

您可以使用 SpannableString

SpannableString ss = new SpannableString("Hi this is #Naveen. I'll meet #Peter in the evening.. Would you like to join #Sam??");
        ClickableSpan clickableSpanNaveen = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Do Stuff for naveen
            }
        };
        ClickableSpan clickableSpanPeter = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Do Stuff for peter
            }
        };
        ClickableSpan clickableSpanSam = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Do Stuff for sam
            }
        };

        ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
        contentTextView.setText(ss);
        contentTextView.setMovementMethod(LinkMovementMethod.getInstance());

我从here复制这段代码

但你必须找到一种方法来检测每个跨度的开始和结束。

  • 一开始,您可以循环字符串并使用indexOf("#"),并将这些索引存储在例如数组中。
  • 对于结尾,您可能使用前一个数组(其中存储了# 的索引)并尝试使用indexOf(" ", // index of "#" //) 找到他之后的第一个空格“”。 (注意:此逻辑仅在所有可点击字符串中都没有空格时才有效)

【讨论】:

    【解决方案2】:

    您好,您应该使用 ClickableSpan.. 让我通过示例向您解释一下.. myString ="点击这里阅读更多"

    我想让“这里”这个词可以点击(蓝色)..

        SpannableString ReadMore = new SpannableString(myString);
    
                ClickableSpan clickReadMore = new ClickableSpan() {
                    @Override
                    public void onClick(View textView) {
    
                        //do your stuff here when "here" is clicked
                    }
                };
    
    //  below 6 is start index of clickable string and 10 is end index of clickable string
                ReadMore.setSpan(clickReadMore, 6, 10,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
                myTextView.setText(ReadMore);
                myTextView.setMovementMethod(LinkMovementMethod
                        .getInstance());
    

    使用上面的示例,您可以拆分字符串并使用单独的 ClickableSpan 使它们可点击

    【讨论】:

      【解决方案3】:

      这是一种处理输入以准备从标签创建超链接的方法。

      import java.util.ArrayList;
      import java.util.regex.*;
      
      public class Hashtag {  
          public static void main(String[] args) {
              String input = "#outfit#fashion#color\n"
                  + "#outfit\n"
                  + "#outfit is really awsome.\n"
                  + "check my new #dress, the brand #new #outfit. Isn't it #cool?\n";
      
              Pattern pattern = Pattern.compile("#([a-zA-Z]+)");
              Matcher matcher = pattern.matcher(input);
      
              // will store the parsed input string
              ArrayList<Text> parsed = new ArrayList<Text>();
      
              // populate the ArrayList
              int previousPosition = 0;
              while (matcher.find()) {
                  String previousText = input.substring(previousPosition, matcher.start());
                  if (!previousText.isEmpty()) {
                      parsed.add(new Text(previousText));
                  }
                  parsed.add(new Tag(matcher.group(1)));
                  previousPosition = matcher.end();
              }
              String trailingText = input.substring(previousPosition, input.length());
              if (!trailingText.isEmpty()) {
                  parsed.add(new Text(trailingText));
              }
      
              // the input string 
              for (Text t : parsed) {
                  if (t instanceof Tag) {
                      Tag tag = (Tag)t;
      
                      /* Here you would do some special handling of the tags to make them clickable
                       * tag.getTag(); can be used to get the contents of tag. For example, the 
                       * for a "#OutFit" tag the getTag() method returns "outfit"
                       */
      
                      System.out.print("[" + tag + "]");
                  } else {
                      System.out.print(t);
                  }
              }
          }
      
          public static class Text {
              private String text;
      
              Text(String text) {
                  this.text = text;
              }
      
              @Override
              public String toString() {
                  return text;
              }
          }
      
          public static class Tag extends Text
          {
              private String tag;
              Tag(String tag) {
                  super("#" + tag);
                  this.tag = tag.toLowerCase();
              }
      
              public String getTag() {
                  return tag;
              }
          }
      }
      

      以上代码产生以下输出。它将标签放在方括号中以证明提取有效。

      [#outfit][#fashion][#color]
      [#outfit]
      [#outfit] is really awsome.
      check my new [#dress], the brand [#new] [#outfit]. Isn't it [#cool]?
      

      从这一点来看,显示文本和使标签可点击应该很容易。

      【讨论】:

        【解决方案4】:

        经过长时间的奋斗,我找到了精彩的教程:

        how-to-implment-hashtags-and-callouts-in-android

        @Rami 的答案适用于静态字符串,但本教程适用于动态数据。 希望对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-12
          • 1970-01-01
          • 2018-01-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多