【问题标题】:Make a wavy red underline for some words in edittext为edittext中的某些单词添加波浪形红色下划线
【发布时间】:2013-05-22 23:36:21
【问题描述】:

我正在制作一个代码编辑器,如果用户在编写代码时出现错误,我正在尝试动态添加红色波浪下划线。我尝试使用 underlineSpan,但我不知道如何使它呈波浪状。使用 .setColor() 更改颜色似乎也不起作用。是否有 Span 可以帮助我,或者有没有办法通过 Paint 来实现?

【问题讨论】:

  • 我通过编写自定义跨度实现了这一点,请在此处查看解决方案stackoverflow.com/a/27270036/519995
  • 感谢您的回答,我稍后会尝试实现它,看看它是否有效。

标签: android android-edittext html underline


【解决方案1】:

您可以使用TextPaint

TextPaint tp = new TextPaint();
tp.linkColor = Color.BLACK;  //you can use your color using Color.parseColor("#123456");   
UnderlineSpan us = new UnderlineSpan();
us.updateDrawState(tp);
SpannableString sp = new SpannableString("Welcome to android");
sp.setSpan(us, 11, 18, 0); // UnderlineSpan Object, Start position, End position, Flag.
et.setText(sp);

这对我有用。我希望这会对你有所帮助。

【讨论】:

  • 感谢您的回答,不幸的是,这不会改变我的下划线颜色。
  • 你能告诉我一些你尝试使用 underlinespan 的代码吗?
  • Spannable span = new SpannableString(codeField.getText()); TextPaint tp = new TextPaint(); tp.linkColor = Color.parseColor("#FF0000"); UnderlineSpan undrlspan = new UnderlineSpan(); undrlspan.updateDrawState(tp); span.setSpan(undrlspan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); codeField.setText(span); 很抱歉这种格式。第一次在这里写问题。其中 codeField 是包含用户代码的 EditText。
  • 我查了一下,textPaint的颜色变成了红色,但是对下划线本身没有影响
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
相关资源
最近更新 更多