【问题标题】:how to add text with format into edit text?如何将带有格式的文本添加到编辑文本中?
【发布时间】:2013-01-11 03:54:14
【问题描述】:
图片来自名为kakao story 的应用。
假设有一个帖子,其中包含与任何 sns 应用程序一样的 cmets 列表。
当您单击评论时,它会在编辑文本中插入评论者的用户名以指示 my new comment is a reply to the user。
(您不能多次添加相同的名称。)
当您按退格键删除名称时,组成名称的整个字符(例如,示例中的 chabeau)将被 1-退格键删除。
我正在尝试模仿这种行为,并想要一些指示如何实现它或搜索什么。
【问题讨论】:
标签:
android
android-edittext
【解决方案1】:
如果您正在寻找气泡视图。您可以通过创建android.text.style.DynamicDrawableSpan.ImageSpan 的子类来实现它,它将EditText 字符串的一部分转换为格式化的span。
SO Question 将为您提供有关创建格式化跨度的一些基本概念。
This 是使用spans 自定义editext 的好教程。
对于一次删除整个单词,您可以使用SPAN_EXCLUSIVE_EXCLUSIVE 属性。
下面的代码将格式化字符串的前四个字符,希望能给你一些提示。
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
final ForegroundColorSpan fcs
= new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to set text color to some RGB value
sb.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(sb);
【解决方案2】:
EditText et = (EditText) findViewById(R.id.edit1);
et.setTextColor(Color.parseColor("yourColorCodeHere"));