【发布时间】:2017-03-13 21:14:17
【问题描述】:
从 android docs 开始,TextView、EDITABLE、NORMAL 和 SPANNABLE 有 3 种类型的缓冲区。它们各自有什么区别,它们的一些常见用例是什么?
【问题讨论】:
标签: android android-edittext textview
从 android docs 开始,TextView、EDITABLE、NORMAL 和 SPANNABLE 有 3 种类型的缓冲区。它们各自有什么区别,它们的一些常见用例是什么?
【问题讨论】:
标签: android android-edittext textview
TextView.BufferType 将用于在运行时更改 TextView,例如插入、在单个 TextView 中设置不同的颜色、样式等。
EDITABLE -> 只返回 Spannable 和 Editable。
NORMAL ->只返回任何字符序列。
SPANNABLE -> 只返回 Spannable。
这里是TextView.BufferType.EDITABLE 使用。
yourTextView.setText("is a textView of Editable BufferType",TextView.BufferType.EDITABLE);
/* here i insert the textView value in a runtime*/
Editable editable = youTextView.getEditableText();
editable.insert(0,"This "); //0 is the index value where the "TEXT" will be placed
输出:
This is a textView of Editable BufferType
这里是TextView.BufferType.SPANNABLE 使用和 这也是TextView.BufferType.EDITABLE 写的(因为可编辑写成spannable 或可编辑)通过更改参数
yourTextView.setText("textView of Spannable BufferType",TextView.BufferType.SPANNABLE);
/* here i change the color in a textview*/
Spannable span = (Spannable)yourTextView.getText();
span.setSpan(new ForegroundColorSpan(0xff0000ff),11,"textView of Spannable BufferType".length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
输出:
textView of `Spannable BufferType`(Spannable BufferType are in blue color)
【讨论】: