The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 27 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | /** |
Joe Fernandez | 38ed689 | 2017-04-25 22:06:48 -0700 | [diff] [blame] | 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> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 47 | * |
Joe Fernandez | 38ed689 | 2017-04-25 22:06:48 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | * <p> |
Andrei Stingaceanu | 5d04239 | 2017-01-17 14:00:41 +0000 | [diff] [blame] | 66 | * This widget does not support auto-sizing text. |
| 67 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
Philip Milne | ab104ba | 2013-04-19 03:53:38 +0000 | [diff] [blame] | 74 | public class EditText extends TextView { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 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); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Siyamed Sinir | 0277119 | 2016-02-05 16:08:59 -0800 | [diff] [blame] | 92 | public boolean getFreezesText() { |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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() { |
Clara Bayarri | c832bd5 | 2018-01-02 15:32:18 +0000 | [diff] [blame] | 108 | CharSequence text = super.getText(); |
Clara Bayarri | b102cc2 | 2018-01-18 12:36:00 +0000 | [diff] [blame] | 109 | // This can only happen during construction. |
| 110 | if (text == null) { |
| 111 | return null; |
| 112 | } |
Clara Bayarri | c832bd5 | 2018-01-02 15:32:18 +0000 | [diff] [blame] | 113 | if (text instanceof Editable) { |
| 114 | return (Editable) super.getText(); |
| 115 | } |
| 116 | super.setText(text, BufferType.EDITABLE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Siyamed Sinir | 192d3bd | 2016-03-02 14:11:34 -0800 | [diff] [blame] | 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 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 171 | |
| 172 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 173 | public CharSequence getAccessibilityClassName() { |
| 174 | return EditText.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 175 | } |
Guang Zhu | 4cd353c | 2014-02-12 19:54:30 -0800 | [diff] [blame] | 176 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 177 | /** @hide */ |
Guang Zhu | 4cd353c | 2014-02-12 19:54:30 -0800 | [diff] [blame] | 178 | @Override |
Andrei Stingaceanu | 5038d58 | 2016-11-28 15:51:13 +0000 | [diff] [blame] | 179 | protected boolean supportsAutoSizeText() { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | /** @hide */ |
| 184 | @Override |
Svetoslav | f4b1ada | 2015-11-09 11:57:14 -0800 | [diff] [blame] | 185 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |