【发布时间】:2017-01-14 23:23:42
【问题描述】:
我正在使用自定义 EditText 类,该类支持通过用户输入为特定小部件输入图像跨度,但遇到了一个奇怪的问题。当图像跨度出现在一行的末尾时,转移到下一行有时会导致图像跨度不再可见。
基本上,在将图像跨度移动到多行编辑文本中的下一行时,编辑文本似乎没有正确处理图像跨度。它只是在被结转后对用户不可见。我可以退格第二个图像的内容,直到它看起来与第一个图像完全一样(到“n”)并且我们再次看到图像跨度。
有人知道我可以做些什么来解决这个问题吗?这是我想保留在我的应用程序中的一个关键组件。我也不能回退到单行编辑文本,多行支持也很关键。
为了重现性,这是我将 ImageSpan 添加到我的编辑文本的代码:
public void appendSpannedText(String s){
if (textToDrawableMap == null || textToDrawableMap.isEmpty()
|| !textToDrawableMap.containsKey(s)) {
return;
}
// Acquire the mapped drawable
Drawable drawable = textToDrawableMap.get(s);
Editable editable = getText();
int start = getSelectionStart();
// Insert the a space at the start that's eaten by the image span
// being set.
editable = editable.insert(start, SPACE);
// Insert the new string at the starting point
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(editable);
// Create the span and set the new span to the appropriate range
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
int nextIdx = start + 1;
spannableStringBuilder.setSpan(span,
start, nextIdx,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the spanned text and the cursor
setText(spannableStringBuilder);
setSelection(nextIdx);
}
我只有几个用户可以按下的图标,它通过 spannable 将图像跨度插入到自定义编辑文本中。我使用我支持的文本映射 -> 可绘制对象,因此我知道当用户按下特定按钮时要使用哪个。主要的是,如果您在编辑文本中有一个图像跨度,则在触发时它会在新行上变得不可见。
【问题讨论】:
标签: android html android-edittext