Various of setText (CharSequence, TextView.BufferType) and setText (CharSequence)

what is the difference between setText(CharSequence, TextView.BufferType)and setText(CharSequence), and when should we use them?

+4
source share
3 answers
setText (CharSequence text)

Sets the string value of a TextView. whereas

setText (CharSequence text, TextView.BufferType type) 

Sets the text displayed by this TextView, and also establishes whether it is stored in the buffer with the ability to erase / span and whether its editable is available.

All parameters of BufferType:

  • TextView.BufferType.EDITABLE
  • TextView.BufferType.NORMAL
  • TextView.BufferType.SPANNABLE

eg.

myEditText.setText("This is new text from setText with BufferType EDITABLE.",  TextView.BufferType.EDITABLE); 
0
source

You can see the difference from the text code.

  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);
    }

If you use a simple setText by default, it takes the type TextView.BufferType.NORMAL, which is basically either a simple string for a SpannedString.

0
source

Source: https://habr.com/ru/post/1584604/


All Articles