Java setText(CharSequence,TextView.BufferType)和setText(CharSequence)的不同
Java setText(CharSequence,TextView.BufferType)和setText(CharSequence)的不同,java,android,Java,Android,setText(CharSequence,TextView.BufferType)和setText(CharSequence)之间有什么区别,我们应该在什么时候使用它们?根据文档,区别在于setText(CharSequence,TextView.BufferType)还设置文本是否存储在可设置样式的缓冲区中,以及文本是否可编辑 设置TextView的字符串值。鉴于 setText (CharSequence text, TextView.BufferType type) 设置此TextV
setText(CharSequence,TextView.BufferType)
和setText(CharSequence)
之间有什么区别,我们应该在什么时候使用它们?根据文档,区别在于setText(CharSequence,TextView.BufferType)
还设置文本是否存储在可设置样式的缓冲区中,以及文本是否可编辑
设置TextView的字符串值。鉴于
setText (CharSequence text, TextView.BufferType type)
设置此TextView要显示的文本,还设置它是否存储在可设置样式的缓冲区中以及是否可编辑
所有缓冲区类型选项都是:
myEditText.setText("This is new text from setText with BufferType EDITABLE.", TextView.BufferType.EDITABLE);
您可以从textview代码中看到差异
if (type == BufferType.EDITABLE || getKeyListener() != null ||
needEditableForNotification) {
createEditorIfNeeded();
Editable t = mEditableFactory.newEditable(text);
text = t;
setFilters(t, mFilters);
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) imm.restartInput(this);
} else if (type == BufferType.SPANNABLE || mMovement != null) {
text = mSpannableFactory.newSpannable(text);
} else if (!(text instanceof CharWrapper)) {
text = TextUtils.stringOrSpannedString(text);
}
如果默认情况下使用纯setText,则它将采用TextView.BufferType.NORMAL类型,基本上是纯字符串或纯字符串来跨越字符串。最后一句话并不完全正确,默认情况下,setText调用setText(text,mBufferType)。虽然mBufferType默认为BufferType.NORMAL,但它是在setText(CharSequence,BufferType)中设置的,构造函数会调用它。TLDR:setText默认为最近使用的缓冲区类型,通常是构建TextView的缓冲区类型。我相信这个答案的示例代码,结合@devconsole的注释和更全面的解释会更有用。我认为这个答案解释的不多。它只是呼应了三个可能值的名称。下面对Manish的答案稍加编辑,再加上devconsole的评论,会更有启发性。@m_katsifarakis请检查这个答案和Manish的答案的时间。@Girubai当然他们几乎是同时发布的,但评论是针对OP的,这样他可以更好地选择一个被接受的答案。
if (type == BufferType.EDITABLE || getKeyListener() != null ||
needEditableForNotification) {
createEditorIfNeeded();
Editable t = mEditableFactory.newEditable(text);
text = t;
setFilters(t, mFilters);
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) imm.restartInput(this);
} else if (type == BufferType.SPANNABLE || mMovement != null) {
text = mSpannableFactory.newSpannable(text);
} else if (!(text instanceof CharWrapper)) {
text = TextUtils.stringOrSpannedString(text);
}