how to insert image to a editText(如何将图像插入editText)
问题描述
我想将图像插入到 editText 我的代码是:
I want to insert a image to a editText my code is:
CharSequence charSeq= editText.getText()+" ";
SpannableString ss2 = new SpannableString(charSeq);
Drawable d2 = holder.image.getDrawable();
d2.setBounds(0, 0, d2.getIntrinsicWidth(), d2.getIntrinsicHeight());
ImageSpan span2 = new ImageSpan(d2, ImageSpan.ALIGN_BASELINE);
ss2.setSpan(span2,charSeq.length()-1, charSeq.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
editText.setText(ss2,BufferType.SPANNABLE);
我的代码可以运行,但我有一些不错的经验我想修改:
My code can run but i have some not bad experience i want to modify:
1:你知道当使用ss2.setSpan()方法时,图片可以替换字符,我只想插入新图片,不想图片替换字符.
1: You know when use ss2.setSpan() method, the image can replace the character, i only want to insert new image, donot want to the image replace the character.
2:你知道我的方法包括editText.getText()+";",我添加了一些额外的空间,以便图像可以插入到 CharSequence 的最后.怎么不用添加一些Extra,图片也插入到CharSequence的最后.
2: you know my method include "editText.getText()+" ";", i add some Extra space, so that the image can insert to the last of the CharSequence. how to not need add add some Extra, the image also insert to the last of the CharSequence.
3.当我将图像插入到 CharSequence 的最后时,光标不在最后,它出现在 CharSequence 的前面.如何将光标放在图像的后面.
3.when i insert the image to the last of the CharSequence, the cursor not at the last, it appear in the front of the CharSequence. how to put the cursor at the behind the image.
4.我想不断的在不同的CharSequence中插入图片,怎么办?
4.i want to constantly insert the image in the different of the CharSequence,how to do?
我的问题太多了,希望你能帮助我,非常感谢.
My question so many, I want you can help me thank you very very much.
推荐答案
做这样的事情(注意:可以复用 SpannableStringBuilder)
Do something like this (note: you can reuse SpannableStringBuilder)
editText = (EditText)mRoot.findViewById(R.id.content);
ImageSpan imageSpan = new ImageSpan(preview);
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(editText.getText());
// this is a string that will let you find a place, where the ImageSpan is.
String imgId = "[img=1]";
int selStart = editText.getSelectionStart();
// current selection is replaceв with imageId
builder.replace(editText.getSelectionStart(), editText.getSelectionEnd(), imgId);
// This adds a span to display image where the imageId is. If you do builder.toString() - the string will contain imageId where the imageSpan is.
// you can use it later - if you want to find location of imageSpan in text;
builder.setSpan(imageSpan, selStart, selStart + imgId.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(builder);
注意:请参阅后续回答处理部分删除标签
Note: See follow up answer for dealing with partial deletion of tags
这篇关于如何将图像插入editText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将图像插入editText


- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01