ros*_*hal 9 html android android-edittext
我正在使用一个自定义的 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);
}
Run Code Online (Sandbox Code Playgroud)
我只有一些用户可以按下的图标,它通过可跨度将图像跨度插入到自定义编辑文本中。我使用了我支持的文本映射 -> 可绘制对象,因此我知道当用户按下特定按钮时要使用哪个。最重要的是,如果您在 edittext 中有一个图像跨度,则触发时它可能会在新行上变得不可见。
小智 5
我在EditText 上遇到了同样的问题。
正如之前的评论所说,这是因为空格字符。当EditText变成一个新行时,组件会删除上一行的最后一个字符(如果这是一个空格)。因此,为了避免这种情况发生,我们必须使用不同的方法。
在之前的代码中,我们有这样的:
editable = editable.insert(start, SPACE);
Run Code Online (Sandbox Code Playgroud)
取而代之的是,我们应该编写下一个代码:
editable = editable.insert(start, anyCharacter);
Run Code Online (Sandbox Code Playgroud)
其中anyCharacter是与“”不同的字符。
| 归档时间: |
|
| 查看次数: |
1277 次 |
| 最近记录: |