| 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.graphics.Rect; |
| 21 | import android.text.Editable; |
| 22 | import android.text.Selection; |
| 23 | import android.text.Spannable; |
| 24 | import android.text.TextUtils; |
| 25 | import android.text.method.ArrowKeyMovementMethod; |
| 26 | import android.text.method.MovementMethod; |
| 27 | import android.util.AttributeSet; |
| 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 | * <EditText |
| 43 | * android:id="@+id/plain_text_input" |
| 44 | * android:layout_height="wrap_content" |
| 45 | * android:layout_width="match_parent" |
| 46 | * android:inputType="text"/></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 { |
| 75 | 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 | |
| 83 | 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); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean getFreezesText() { |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | 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() { |
| 108 | CharSequence text = super.getText(); |
| 109 | // This can only happen during construction. |
| 110 | if (text == null) { |
| 111 | return null; |
| 112 | } |
| 113 | if (text instanceof Editable) { |
| 114 | return (Editable) text; |
| 115 | } |
| 116 | super.setText(text, BufferType.EDITABLE); |
| 117 | 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 | |
| 153 | /** |
| 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 | */ |
| 163 | @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 | } |
| 171 | |
| 172 | @Override |
| 173 | public CharSequence getAccessibilityClassName() { |
| 174 | return EditText.class.getName(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 179 | super.onSizeChanged(w, h, oldw, oldh); |
| 180 | setHandwritingArea(new Rect(0, 0, w, h)); |
| 181 | } |
| 182 | |
| 183 | /** @hide */ |
| 184 | @Override |
| 185 | protected boolean supportsAutoSizeText() { |
| 186 | return false; |
| 187 | } |
| 188 | } |