• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.widget;
18 
19 import android.content.Context;
20 import android.text.Editable;
21 import android.text.Selection;
22 import android.text.Spannable;
23 import android.text.TextUtils;
24 import android.text.method.ArrowKeyMovementMethod;
25 import android.text.method.MovementMethod;
26 import android.util.AttributeSet;
27 import android.view.accessibility.AccessibilityNodeInfo;
28 
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 /**
36  * 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>
47  *
48  * 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>
65  * <p>
66  * This widget does not support auto-sizing text.
67  * <p>
68  * <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  */
74 public class EditText extends TextView {
EditText(Context context)75     public EditText(Context context) {
76         this(context, null);
77     }
78 
EditText(Context context, AttributeSet attrs)79     public EditText(Context context, AttributeSet attrs) {
80         this(context, attrs, com.android.internal.R.attr.editTextStyle);
81     }
82 
EditText(Context context, AttributeSet attrs, int defStyleAttr)83     public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
84         this(context, attrs, defStyleAttr, 0);
85     }
86 
EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)87     public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
88         super(context, attrs, defStyleAttr, defStyleRes);
89     }
90 
91     @Override
getFreezesText()92     public boolean getFreezesText() {
93         return true;
94     }
95 
96     @Override
getDefaultEditable()97     protected boolean getDefaultEditable() {
98         return true;
99     }
100 
101     @Override
getDefaultMovementMethod()102     protected MovementMethod getDefaultMovementMethod() {
103         return ArrowKeyMovementMethod.getInstance();
104     }
105 
106     @Override
getText()107     public Editable getText() {
108         return (Editable) super.getText();
109     }
110 
111     @Override
setText(CharSequence text, BufferType type)112     public void setText(CharSequence text, BufferType type) {
113         super.setText(text, BufferType.EDITABLE);
114     }
115 
116     /**
117      * Convenience for {@link Selection#setSelection(Spannable, int, int)}.
118      */
setSelection(int start, int stop)119     public void setSelection(int start, int stop) {
120         Selection.setSelection(getText(), start, stop);
121     }
122 
123     /**
124      * Convenience for {@link Selection#setSelection(Spannable, int)}.
125      */
setSelection(int index)126     public void setSelection(int index) {
127         Selection.setSelection(getText(), index);
128     }
129 
130     /**
131      * Convenience for {@link Selection#selectAll}.
132      */
selectAll()133     public void selectAll() {
134         Selection.selectAll(getText());
135     }
136 
137     /**
138      * Convenience for {@link Selection#extendSelection}.
139      */
extendSelection(int index)140     public void extendSelection(int index) {
141         Selection.extendSelection(getText(), index);
142     }
143 
144     /**
145      * Causes words in the text that are longer than the view's width to be ellipsized instead of
146      * broken in the middle. {@link TextUtils.TruncateAt#MARQUEE
147      * TextUtils.TruncateAt#MARQUEE} is not supported.
148      *
149      * @param ellipsis Type of ellipsis to be applied.
150      * @throws IllegalArgumentException When the value of <code>ellipsis</code> parameter is
151      *      {@link TextUtils.TruncateAt#MARQUEE}.
152      * @see TextView#setEllipsize(TextUtils.TruncateAt)
153      */
154     @Override
setEllipsize(TextUtils.TruncateAt ellipsis)155     public void setEllipsize(TextUtils.TruncateAt ellipsis) {
156         if (ellipsis == TextUtils.TruncateAt.MARQUEE) {
157             throw new IllegalArgumentException("EditText cannot use the ellipsize mode "
158                     + "TextUtils.TruncateAt.MARQUEE");
159         }
160         super.setEllipsize(ellipsis);
161     }
162 
163     @Override
getAccessibilityClassName()164     public CharSequence getAccessibilityClassName() {
165         return EditText.class.getName();
166     }
167 
168     /** @hide */
169     @Override
supportsAutoSizeText()170     protected boolean supportsAutoSizeText() {
171         return false;
172     }
173 
174     /** @hide */
175     @Override
onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)176     public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
177         super.onInitializeAccessibilityNodeInfoInternal(info);
178         if (isEnabled()) {
179             info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_TEXT);
180         }
181     }
182 }
183