Únete a ⁠ #Android11: The Beta Launch Show el 3 de junio.

AppCompatEditText

public class AppCompatEditText
extends EditText implements TintableBackgroundView

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.EditText
         ↳ androidx.appcompat.widget.AppCompatEditText


A EditText which supports compatible features on older versions of the platform, including:

  • Allows dynamic tint of its background via the background tint methods in ViewCompat.
  • Allows setting of the background tint using R.attr.backgroundTint and R.attr.backgroundTintMode.
  • Allows setting a custom receiver callback in order to handle insertion of content (e.g. pasting text or an image from the clipboard). This callback provides the opportunity to implement app-specific handling such as creating an attachment when an image is pasted.

This will automatically be used when you use EditText in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.

Summary

Inherited constants

Inherited fields

Public constructors

AppCompatEditText(Context context)
AppCompatEditText(Context context, AttributeSet attrs)
AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

RichContentReceiverCompat<TextView> getRichContentReceiverCompat()

Returns the callback that handles insertion of content into this view (e.g.

Editable getText()

Return the text that the view is displaying.

TextClassifier getTextClassifier()

Returns the TextClassifier used by this TextView.

InputConnection onCreateInputConnection(EditorInfo outAttrs)

If a receiver callback is set, the returned InputConnection will use it to handle calls to InputConnection.commitContent(InputContentInfo, int, Bundle).

boolean onTextContextMenuItem(int id)

If a receiver callback is set, uses it to execute the "Paste" and "Paste as plain text" menu actions.

void setBackgroundDrawable(Drawable background)
void setBackgroundResource(int resId)
void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback)

See TextViewCompat.setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

void setRichContentReceiverCompat(RichContentReceiverCompat<TextView> receiver)

Sets the callback to handle insertion of content into this view.

void setTextAppearance(Context context, int resId)
void setTextClassifier(TextClassifier textClassifier)

Sets the TextClassifier for this TextView.

Protected methods

void drawableStateChanged()

Inherited methods

Public constructors

AppCompatEditText

public AppCompatEditText (Context context)

Parameters
context Context

AppCompatEditText

public AppCompatEditText (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

AppCompatEditText

public AppCompatEditText (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

getRichContentReceiverCompat

public RichContentReceiverCompat<TextView> getRichContentReceiverCompat ()

Returns the callback that handles insertion of content into this view (e.g. pasting from the clipboard). See setRichContentReceiverCompat(RichContentReceiverCompat) for more info.

Returns
RichContentReceiverCompat<TextView> The callback that this view is using to handle insertion of content. Returns null if no callback is configured, in which case the platform behavior of the EditText component will be used for content insertion.

getText

public Editable getText ()

Return the text that the view is displaying. If an editable text has not been set yet, this will return null.

Returns
Editable

getTextClassifier

public TextClassifier getTextClassifier ()

Returns the TextClassifier used by this TextView. If no TextClassifier has been set, this TextView uses the default set by the TextClassificationManager.

Returns
TextClassifier

onCreateInputConnection

public InputConnection onCreateInputConnection (EditorInfo outAttrs)

If a receiver callback is set, the returned InputConnection will use it to handle calls to InputConnection.commitContent(InputContentInfo, int, Bundle).

Parameters
outAttrs EditorInfo

Returns
InputConnection

onTextContextMenuItem

public boolean onTextContextMenuItem (int id)

If a receiver callback is set, uses it to execute the "Paste" and "Paste as plain text" menu actions.

Parameters
id int

Returns
boolean

setBackgroundDrawable

public void setBackgroundDrawable (Drawable background)

Parameters
background Drawable

setBackgroundResource

public void setBackgroundResource (int resId)

Parameters
resId int

setCustomSelectionActionModeCallback

public void setCustomSelectionActionModeCallback (ActionMode.Callback actionModeCallback)

See TextViewCompat.setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

Parameters
actionModeCallback ActionMode.Callback

setRichContentReceiverCompat

public void setRichContentReceiverCompat (RichContentReceiverCompat<TextView> receiver)

Sets the callback to handle insertion of content into this view.

"Content" and "rich content" here refers to both text and non-text: plain text, styled text, HTML, images, videos, audio files, etc. The callback configured here should typically extend from TextViewRichContentReceiverCompat to provide consistent behavior for text content.

This callback will be invoked for the following scenarios:

  1. Paste from the clipboard (e.g. "Paste" or "Paste as plain text" action in the insertion/selection menu)
  2. Content insertion from the keyboard (InputConnection.commitContent(InputContentInfo, int, Bundle))

Parameters
receiver RichContentReceiverCompat: The callback to use. This can be null to clear any previously set callback (the platform behavior of the EditText component will then be used).

setTextAppearance

public void setTextAppearance (Context context, 
                int resId)

Parameters
context Context

resId int

setTextClassifier

public void setTextClassifier (TextClassifier textClassifier)

Sets the TextClassifier for this TextView.

Parameters
textClassifier TextClassifier

Protected methods

drawableStateChanged

protected void drawableStateChanged ()