javascript - 选定文本的非粗体部分不起作用
问题描述
我试图找到一个答案,将文本的选定部分从其他非粗体文本中展开,但它正在将行中的整个文本展开,如图所示。我多次发布了这个问题,但没有人可以为我提供正确的答案。我正在取消“我正在使用”这句话,但是当点击它时,它会取消整行。请帮忙。
这是我正在使用的代码。
private void boldText(){
int selectionStartb = texto.getSelectionStart();
int selectionEndb = texto.getSelectionEnd();
if (selectionStartb > selectionEndb) {
int temp = selectionEndb;
selectionEndb = selectionStartb;
selectionStartb = temp;
}
if (selectionEndb > selectionStartb) {
Spannable str = texto.getText();
boolean BL = false;
StyleSpan[] styleSpans;
styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
// If the selected text-part already has BOLD style on it, then
// we need to disable it
for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
str.removeSpan(styleSpans[i]);
BL = true;
}
}
// Else we set BOLD style on it
if (!BL) {
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
}
}
解决方案
推荐阅读
- postgresql - 是否可以更改 PostgreSQL 中的多个列?
- python - 如果主要版本为 0,则没有预发布标签
- javascript - 将字符串转换为对象数组
- python - BreezypythonGui 程序正在运行但没有显示窗口?
- excel - Excel 动态偏移函数
- java - 字段“状态”上的对象“标题”中的字段错误:拒绝值 [Pendente];
- java - Jmeter maven插件跳过单元测试
- python - Python:如何使用 ffill() 并将计算存储在 if 条件中
- azure-policy - 检查 azure 策略中“名称”字段中的几个可能值
- python - 使用 np.kron 生成大矩阵时如何避免内存错误