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 28 /* 29 * This is supposed to be a *very* thin veneer over TextView. 30 * Do not make any changes here that do anything that a TextView 31 * with a key listener and a movement method wouldn't do! 32 */ 33 34 /** 35 * A user interface element for entering and modifying text. 36 * When you define an edit text widget, you must specify the 37 * {@link android.R.styleable#TextView_inputType} 38 * attribute. For example, for plain text input set inputType to "text": 39 * <p> 40 * <pre> 41 * <EditText 42 * android:id="@+id/plain_text_input" 43 * android:layout_height="wrap_content" 44 * android:layout_width="match_parent" 45 * android:inputType="text"/></pre> 46 * 47 * Choosing the input type configures the keyboard type that is shown, acceptable characters, 48 * and appearance of the edit text. 49 * For example, if you want to accept a secret number, like a unique pin or serial number, 50 * you can set inputType to "numericPassword". 51 * An inputType of "numericPassword" results in an edit text that accepts numbers only, 52 * shows a numeric keyboard when focused, and masks the text that is entered for privacy. 53 * <p> 54 * See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a> 55 * guide for examples of other 56 * {@link android.R.styleable#TextView_inputType} settings. 57 * </p> 58 * <p>You also can receive callbacks as a user changes text by 59 * adding a {@link android.text.TextWatcher} to the edit text. 60 * This is useful when you want to add auto-save functionality as changes are made, 61 * or validate the format of user input, for example. 62 * You add a text watcher using the {@link TextView#addTextChangedListener} method. 63 * </p> 64 * <p> 65 * This widget does not support auto-sizing text. 66 * <p> 67 * <b>XML attributes</b> 68 * <p> 69 * See {@link android.R.styleable#EditText EditText Attributes}, 70 * {@link android.R.styleable#TextView TextView Attributes}, 71 * {@link android.R.styleable#View View Attributes} 72 */ 73 public class EditText extends TextView { EditText(Context context)74 public EditText(Context context) { 75 this(context, null); 76 } 77 EditText(Context context, AttributeSet attrs)78 public EditText(Context context, AttributeSet attrs) { 79 this(context, attrs, com.android.internal.R.attr.editTextStyle); 80 } 81 EditText(Context context, AttributeSet attrs, int defStyleAttr)82 public EditText(Context context, AttributeSet attrs, int defStyleAttr) { 83 this(context, attrs, defStyleAttr, 0); 84 } 85 EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)86 public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 87 super(context, attrs, defStyleAttr, defStyleRes); 88 } 89 90 @Override getFreezesText()91 public boolean getFreezesText() { 92 return true; 93 } 94 95 @Override getDefaultEditable()96 protected boolean getDefaultEditable() { 97 return true; 98 } 99 100 @Override getDefaultMovementMethod()101 protected MovementMethod getDefaultMovementMethod() { 102 return ArrowKeyMovementMethod.getInstance(); 103 } 104 105 @Override getText()106 public Editable getText() { 107 CharSequence text = super.getText(); 108 // This can only happen during construction. 109 if (text == null) { 110 return null; 111 } 112 if (text instanceof Editable) { 113 return (Editable) super.getText(); 114 } 115 super.setText(text, BufferType.EDITABLE); 116 return (Editable) super.getText(); 117 } 118 119 @Override setText(CharSequence text, BufferType type)120 public void setText(CharSequence text, BufferType type) { 121 super.setText(text, BufferType.EDITABLE); 122 } 123 124 /** 125 * Convenience for {@link Selection#setSelection(Spannable, int, int)}. 126 */ setSelection(int start, int stop)127 public void setSelection(int start, int stop) { 128 Selection.setSelection(getText(), start, stop); 129 } 130 131 /** 132 * Convenience for {@link Selection#setSelection(Spannable, int)}. 133 */ setSelection(int index)134 public void setSelection(int index) { 135 Selection.setSelection(getText(), index); 136 } 137 138 /** 139 * Convenience for {@link Selection#selectAll}. 140 */ selectAll()141 public void selectAll() { 142 Selection.selectAll(getText()); 143 } 144 145 /** 146 * Convenience for {@link Selection#extendSelection}. 147 */ extendSelection(int index)148 public void extendSelection(int index) { 149 Selection.extendSelection(getText(), index); 150 } 151 152 /** 153 * Causes words in the text that are longer than the view's width to be ellipsized instead of 154 * broken in the middle. {@link TextUtils.TruncateAt#MARQUEE 155 * TextUtils.TruncateAt#MARQUEE} is not supported. 156 * 157 * @param ellipsis Type of ellipsis to be applied. 158 * @throws IllegalArgumentException When the value of <code>ellipsis</code> parameter is 159 * {@link TextUtils.TruncateAt#MARQUEE}. 160 * @see TextView#setEllipsize(TextUtils.TruncateAt) 161 */ 162 @Override setEllipsize(TextUtils.TruncateAt ellipsis)163 public void setEllipsize(TextUtils.TruncateAt ellipsis) { 164 if (ellipsis == TextUtils.TruncateAt.MARQUEE) { 165 throw new IllegalArgumentException("EditText cannot use the ellipsize mode " 166 + "TextUtils.TruncateAt.MARQUEE"); 167 } 168 super.setEllipsize(ellipsis); 169 } 170 171 @Override getAccessibilityClassName()172 public CharSequence getAccessibilityClassName() { 173 return EditText.class.getName(); 174 } 175 176 /** @hide */ 177 @Override supportsAutoSizeText()178 protected boolean supportsAutoSizeText() { 179 return false; 180 } 181 } 182