blob: 728824c2f0dc7b34bfe2cc806d00c6bd61d814d0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.Context;
Gilles Debunnef5c6eff2010-02-09 19:08:36 -080020import android.text.Editable;
21import android.text.Selection;
22import android.text.Spannable;
23import android.text.TextUtils;
24import android.text.method.ArrowKeyMovementMethod;
25import android.text.method.MovementMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.AttributeSet;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080027import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29/*
30 * This is supposed to be a *very* thin veneer over TextView.
31 * Do not make any changes here that do anything that a TextView
32 * with a key listener and a movement method wouldn't do!
33 */
34
35/**
Joe Fernandez38ed6892017-04-25 22:06:48 -070036 * A user interface element for entering and modifying text.
37 * When you define an edit text widget, you must specify the
38 * {@link android.R.styleable#TextView_inputType}
39 * attribute. For example, for plain text input set inputType to "text":
40 * <p>
41 * <pre>
42 * &lt;EditText
43 * android:id="@+id/plain_text_input"
44 * android:layout_height="wrap_content"
45 * android:layout_width="match_parent"
46 * android:inputType="text"/&gt;</pre>
Scott Main41ec6532010-08-19 16:57:07 -070047 *
Joe Fernandez38ed6892017-04-25 22:06:48 -070048 * Choosing the input type configures the keyboard type that is shown, acceptable characters,
49 * and appearance of the edit text.
50 * For example, if you want to accept a secret number, like a unique pin or serial number,
51 * you can set inputType to "numericPassword".
52 * An inputType of "numericPassword" results in an edit text that accepts numbers only,
53 * shows a numeric keyboard when focused, and masks the text that is entered for privacy.
54 * <p>
55 * See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a>
56 * guide for examples of other
57 * {@link android.R.styleable#TextView_inputType} settings.
58 * </p>
59 * <p>You also can receive callbacks as a user changes text by
60 * adding a {@link android.text.TextWatcher} to the edit text.
61 * This is useful when you want to add auto-save functionality as changes are made,
62 * or validate the format of user input, for example.
63 * You add a text watcher using the {@link TextView#addTextChangedListener} method.
64 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 * <p>
Andrei Stingaceanu5d042392017-01-17 14:00:41 +000066 * This widget does not support auto-sizing text.
67 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * <b>XML attributes</b>
69 * <p>
70 * See {@link android.R.styleable#EditText EditText Attributes},
71 * {@link android.R.styleable#TextView TextView Attributes},
72 * {@link android.R.styleable#View View Attributes}
73 */
Philip Milneab104ba2013-04-19 03:53:38 +000074public class EditText extends TextView {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 public EditText(Context context) {
76 this(context, null);
77 }
78
79 public EditText(Context context, AttributeSet attrs) {
80 this(context, attrs, com.android.internal.R.attr.editTextStyle);
81 }
82
Alan Viverette617feb92013-09-09 18:09:13 -070083 public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
84 this(context, attrs, defStyleAttr, 0);
85 }
86
87 public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
88 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
90
91 @Override
Siyamed Sinir02771192016-02-05 16:08:59 -080092 public boolean getFreezesText() {
93 return true;
94 }
95
96 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 protected boolean getDefaultEditable() {
98 return true;
99 }
100
101 @Override
102 protected MovementMethod getDefaultMovementMethod() {
103 return ArrowKeyMovementMethod.getInstance();
104 }
105
106 @Override
107 public Editable getText() {
Clara Bayarric832bd52018-01-02 15:32:18 +0000108 CharSequence text = super.getText();
Clara Bayarrib102cc22018-01-18 12:36:00 +0000109 // This can only happen during construction.
110 if (text == null) {
111 return null;
112 }
Clara Bayarric832bd52018-01-02 15:32:18 +0000113 if (text instanceof Editable) {
114 return (Editable) super.getText();
115 }
116 super.setText(text, BufferType.EDITABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 return (Editable) super.getText();
118 }
119
120 @Override
121 public void setText(CharSequence text, BufferType type) {
122 super.setText(text, BufferType.EDITABLE);
123 }
124
125 /**
126 * Convenience for {@link Selection#setSelection(Spannable, int, int)}.
127 */
128 public void setSelection(int start, int stop) {
129 Selection.setSelection(getText(), start, stop);
130 }
131
132 /**
133 * Convenience for {@link Selection#setSelection(Spannable, int)}.
134 */
135 public void setSelection(int index) {
136 Selection.setSelection(getText(), index);
137 }
138
139 /**
140 * Convenience for {@link Selection#selectAll}.
141 */
142 public void selectAll() {
143 Selection.selectAll(getText());
144 }
145
146 /**
147 * Convenience for {@link Selection#extendSelection}.
148 */
149 public void extendSelection(int index) {
150 Selection.extendSelection(getText(), index);
151 }
152
Siyamed Sinir192d3bd2016-03-02 14:11:34 -0800153 /**
154 * Causes words in the text that are longer than the view's width to be ellipsized instead of
155 * broken in the middle. {@link TextUtils.TruncateAt#MARQUEE
156 * TextUtils.TruncateAt#MARQUEE} is not supported.
157 *
158 * @param ellipsis Type of ellipsis to be applied.
159 * @throws IllegalArgumentException When the value of <code>ellipsis</code> parameter is
160 * {@link TextUtils.TruncateAt#MARQUEE}.
161 * @see TextView#setEllipsize(TextUtils.TruncateAt)
162 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 @Override
164 public void setEllipsize(TextUtils.TruncateAt ellipsis) {
165 if (ellipsis == TextUtils.TruncateAt.MARQUEE) {
166 throw new IllegalArgumentException("EditText cannot use the ellipsize mode "
167 + "TextUtils.TruncateAt.MARQUEE");
168 }
169 super.setEllipsize(ellipsis);
170 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800171
172 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800173 public CharSequence getAccessibilityClassName() {
174 return EditText.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800175 }
Guang Zhu4cd353c2014-02-12 19:54:30 -0800176
Alan Viverettea54956a2015-01-07 16:05:02 -0800177 /** @hide */
Guang Zhu4cd353c2014-02-12 19:54:30 -0800178 @Override
Andrei Stingaceanu5038d582016-11-28 15:51:13 +0000179 protected boolean supportsAutoSizeText() {
180 return false;
181 }
182
183 /** @hide */
184 @Override
Svetoslavf4b1ada2015-11-09 11:57:14 -0800185 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
186 super.onInitializeAccessibilityNodeInfoInternal(info);
187 if (isEnabled()) {
188 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_TEXT);
189 }
190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191}