我有一个句子字符串和一个单词列表,如果该字符串中有一个匹配项,则需要突出显示。 我该怎么做?
答案 0 :(得分:0)
您可以使用Html。
Socket socket = new Socket(...);
socket.setSoTimeout(10000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
[...]
while (true) {
try {
while ((serverSays = in.readLine()) != null) {
System.out.println("Server says: " + serverSays);
}
} catch (SocketTimeoutException e) {
continue;
}
// An exception other than SocketTimeoutException will break the while-loop.
}
// If another exception other than SocketTimeoutException occurs,
// catch it and re-initiate the connection and start over.
答案 1 :(得分:0)
我设法用SpannableStringBuilder做到了。
以下是代码:
@Override
public void renderFinalTranscript(final String transcript, String filler)
{
getActivity().runOnUiThread(() -> {
final Pattern p = Pattern.compile(filler);
final Matcher matcher = p.matcher(transcript);
SpannableStringBuilder sb = new SpannableStringBuilder(transcript);
while(matcher.find()){
sb.setSpan(CharacterStyle.wrap(new ForegroundColorSpan(Color.RED)), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
transcribeText.append(spanny);
});
}
我在for循环中调用了这个方法并获得了预期的结果