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.os.Bundle; 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 import android.view.accessibility.AccessibilityNodeInfo; 29 30 31 /* 32 * This is supposed to be a *very* thin veneer over TextView. 33 * Do not make any changes here that do anything that a TextView 34 * with a key listener and a movement method wouldn't do! 35 */ 36 37 /** 38 * EditText is a thin veneer over TextView that configures itself 39 * to be editable. 40 * 41 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a> 42 * guide.</p> 43 * <p> 44 * <b>XML attributes</b> 45 * <p> 46 * See {@link android.R.styleable#EditText EditText Attributes}, 47 * {@link android.R.styleable#TextView TextView Attributes}, 48 * {@link android.R.styleable#View View Attributes} 49 */ 50 public class EditText extends TextView { EditText(Context context)51 public EditText(Context context) { 52 this(context, null); 53 } 54 EditText(Context context, AttributeSet attrs)55 public EditText(Context context, AttributeSet attrs) { 56 this(context, attrs, com.android.internal.R.attr.editTextStyle); 57 } 58 EditText(Context context, AttributeSet attrs, int defStyleAttr)59 public EditText(Context context, AttributeSet attrs, int defStyleAttr) { 60 this(context, attrs, defStyleAttr, 0); 61 } 62 EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)63 public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 64 super(context, attrs, defStyleAttr, defStyleRes); 65 } 66 67 @Override getFreezesText()68 public boolean getFreezesText() { 69 return true; 70 } 71 72 @Override getDefaultEditable()73 protected boolean getDefaultEditable() { 74 return true; 75 } 76 77 @Override getDefaultMovementMethod()78 protected MovementMethod getDefaultMovementMethod() { 79 return ArrowKeyMovementMethod.getInstance(); 80 } 81 82 @Override getText()83 public Editable getText() { 84 return (Editable) super.getText(); 85 } 86 87 @Override setText(CharSequence text, BufferType type)88 public void setText(CharSequence text, BufferType type) { 89 super.setText(text, BufferType.EDITABLE); 90 } 91 92 /** 93 * Convenience for {@link Selection#setSelection(Spannable, int, int)}. 94 */ setSelection(int start, int stop)95 public void setSelection(int start, int stop) { 96 Selection.setSelection(getText(), start, stop); 97 } 98 99 /** 100 * Convenience for {@link Selection#setSelection(Spannable, int)}. 101 */ setSelection(int index)102 public void setSelection(int index) { 103 Selection.setSelection(getText(), index); 104 } 105 106 /** 107 * Convenience for {@link Selection#selectAll}. 108 */ selectAll()109 public void selectAll() { 110 Selection.selectAll(getText()); 111 } 112 113 /** 114 * Convenience for {@link Selection#extendSelection}. 115 */ extendSelection(int index)116 public void extendSelection(int index) { 117 Selection.extendSelection(getText(), index); 118 } 119 120 /** 121 * Causes words in the text that are longer than the view's width to be ellipsized instead of 122 * broken in the middle. {@link TextUtils.TruncateAt#MARQUEE 123 * TextUtils.TruncateAt#MARQUEE} is not supported. 124 * 125 * @param ellipsis Type of ellipsis to be applied. 126 * @throws IllegalArgumentException When the value of <code>ellipsis</code> parameter is 127 * {@link TextUtils.TruncateAt#MARQUEE}. 128 * @see TextView#setEllipsize(TextUtils.TruncateAt) 129 */ 130 @Override setEllipsize(TextUtils.TruncateAt ellipsis)131 public void setEllipsize(TextUtils.TruncateAt ellipsis) { 132 if (ellipsis == TextUtils.TruncateAt.MARQUEE) { 133 throw new IllegalArgumentException("EditText cannot use the ellipsize mode " 134 + "TextUtils.TruncateAt.MARQUEE"); 135 } 136 super.setEllipsize(ellipsis); 137 } 138 139 @Override getAccessibilityClassName()140 public CharSequence getAccessibilityClassName() { 141 return EditText.class.getName(); 142 } 143 144 /** @hide */ 145 @Override onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)146 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { 147 super.onInitializeAccessibilityNodeInfoInternal(info); 148 if (isEnabled()) { 149 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_TEXT); 150 } 151 } 152 } 153