TextView BufferType

Ger*_*ald 6 android textview android-edittext

从Android 文档中,有3种类型的缓冲区的TextView,EDITABLE,NORMALSPANNABLE.它们之间有什么区别,它们的常见用例是什么?

Rao*_*lan 9

TextView.BufferType将用于更改TextView运行时,如插入、在单个 TextView、样式等中设置不同的颜色。

EDITABLE -> 只返回 Spannable 和 Editable。

NORMAL -> 只返回任何 CharSequence。

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
Run Code Online (Sandbox Code Playgroud)

输出:

This is a textView of Editable BufferType
Run Code Online (Sandbox Code Playgroud)

Here is TextView.BufferType.SPANNABLEuses and This is also TextView.BufferType.EDITABLEwrite by (because editable writer spannable or editable) 通过更改参数

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);
Run Code Online (Sandbox Code Playgroud)

输出:

textView of  `Spannable BufferType`(Spannable BufferType are in blue color)
Run Code Online (Sandbox Code Playgroud)