java - 如何在edittext android中取消加粗和取消突出显示选定的文本
问题描述
嗨,我可以加粗或突出显示选定的文本,但是当尝试在该加粗或突出显示的文本中取消突出显示或取消加粗时,它会取消加粗或取消突出显示整个文本。请告诉我我的代码做错了什么。这是我的代码
private void highlightTextCondition() {
int selectionStart = bodyText.getSelectionStart();
int selectionEnd = bodyText.getSelectionEnd();
if (selectionStart > selectionEnd) {
int temp = selectionEnd;
selectionEnd = selectionStart;
selectionStart = temp;
}
if (selectionEnd > selectionStart) {
Spannable str = bodyText.getText();
boolean exists = false;
for (CharacterStyle span : str.getSpans(selectionStart, selectionEnd, CharacterStyle.class)) {
if (span instanceof BackgroundColorSpan)
str.removeSpan(span);
exists = true;
}
if (!exists) {
str.setSpan(new BackgroundColorSpan(Color.YELLOW), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
bodyText.setSelection(selectionStart, selectionEnd);
}
解决方案
推荐阅读
- sql-server - 对 EF Core Linq 上的子查询求和
- mysql - 用于比较表中两个值的简单内连接
- django - 为什么 django 解析注释 HTML 代码中的标签?
- javascript - 如何自动输入凭据到 javascript 弹出窗口?我试过“webdriver wait”但我得到“TimedOutException”
- jenkins - 我如何使用詹金斯中的构建参数和使用可变参数来触发另一个作业
- material-design - 焦点后材料设计输入文本字段标签位置不正确
- rebus - Rebus Azure 主题名称清理
- asp.net - 在点击事件上过滤数据源
- java - 获取子文档中的数据
- python - 如何在 Django 的 MVT 中正确实现 AJAX