【问题标题】:Android autolinks: Skip numbers having less than 10 digitsAndroid自动链接:跳过少于10位的数字
【发布时间】:2018-11-20 05:02:31
【问题描述】:

我有一个将 autoLinks 设置为 all 的 textview。我想跳过像“2018”这样的数字 这是年份,这些数字不应突出显示。我可以在文本中使用分隔符,以便在解析时跳过这些数字吗?

编辑: 这个问题只发生在小米设备上。

【问题讨论】:

标签: android android-layout


【解决方案1】:

试试这个....

String s1="jan 2018,Saturday";  
String replaceString=s1.replace("2018","");//replaces all occurrences of "2018" to ""  
System.out.println(replaceString);

输出 := jan ,Saturday。

【讨论】:

  • 我已经修改了这个问题,这不是我想要的。我不想跳过文本,我只是希望它不突出显示文本
【解决方案2】:

在这种情况下使用 Spanable String 来突出显示特定字符​​串。

这是一个例子: SpannableString spannableStr = new SpannableString(originalText); ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED); spannableStr.setSpan(foregroundColorSpan, 15, 30, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableTextView.setText(spannableStr);

设置颜色和起始字符串索引和结束索引。

更多详情请查看此链接click this link

【讨论】:

    【解决方案3】:

    我搜索了你的答案试试这个

    private void stripUnderlines(TextView textView) {
        Spannable s = new SpannableString(textView.getText());
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span: spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            s.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);
        }
        textView.setText(s);
    }
    

    这需要自定义版本的 URLSpan,它不启用 TextPaint 的“下划线”属性:

    private class URLSpanNoUnderline extends URLSpan {
        public URLSpanNoUnderline(String url) {
            super(url);
        }
        @Override public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    }  
    

    这里是link

    【讨论】:

      【解决方案4】:

      试试这个代码:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          textView = findViewById(R.id.textView);
          highlightTv();
      
      }
      protected void highlightTv(){
          // Specify the text/word to highlight inside TextView
          String textToHighlight = "2018";
      
          // Construct the formatted text
          String replacedWith = "<font color='green'>" + textToHighlight + "</font>";
      
          // Get the text from TextView
          String originalString = textView.getText().toString();
      
          // Replace the specified text/word with formatted text/word
          String modifiedString = originalString.replaceAll(textToHighlight,replacedWith);
      
          // Update the TextView text
          mTextView.setText(Html.fromHtml(modifiedString));
      }
      

      【讨论】:

      • 我已经修改了这个问题,这不是我想要的。我不想跳过文本,我只是希望它不突出显示文本
      • @rohithammaiah 我更新了我的答案,我仍然不明白你试图解释什么,如果可能的话添加你预期的截图
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多