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 17package android.widget; 18 19import android.content.Context; 20import android.text.Editable; 21import android.text.Selection; 22import android.text.Spannable; 23import android.text.TextUtils; 24import android.text.method.ArrowKeyMovementMethod; 25import android.text.method.MovementMethod; 26import android.util.AttributeSet; 27 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 * EditText is a thin veneer over TextView that configures itself 37 * to be editable. 38 * <p> 39 * <b>XML attributes</b> 40 * <p> 41 * See {@link android.R.styleable#EditText EditText Attributes}, 42 * {@link android.R.styleable#TextView TextView Attributes}, 43 * {@link android.R.styleable#View View Attributes} 44 */ 45public class EditText extends TextView { 46 public EditText(Context context) { 47 this(context, null); 48 } 49 50 public EditText(Context context, AttributeSet attrs) { 51 this(context, attrs, com.android.internal.R.attr.editTextStyle); 52 } 53 54 public EditText(Context context, AttributeSet attrs, int defStyle) { 55 super(context, attrs, defStyle); 56 } 57 58 @Override 59 protected boolean getDefaultEditable() { 60 return true; 61 } 62 63 @Override 64 protected MovementMethod getDefaultMovementMethod() { 65 return ArrowKeyMovementMethod.getInstance(); 66 } 67 68 @Override 69 public Editable getText() { 70 return (Editable) super.getText(); 71 } 72 73 @Override 74 public void setText(CharSequence text, BufferType type) { 75 super.setText(text, BufferType.EDITABLE); 76 } 77 78 /** 79 * Convenience for {@link Selection#setSelection(Spannable, int, int)}. 80 */ 81 public void setSelection(int start, int stop) { 82 Selection.setSelection(getText(), start, stop); 83 } 84 85 /** 86 * Convenience for {@link Selection#setSelection(Spannable, int)}. 87 */ 88 public void setSelection(int index) { 89 Selection.setSelection(getText(), index); 90 } 91 92 /** 93 * Convenience for {@link Selection#selectAll}. 94 */ 95 public void selectAll() { 96 Selection.selectAll(getText()); 97 } 98 99 /** 100 * Convenience for {@link Selection#extendSelection}. 101 */ 102 public void extendSelection(int index) { 103 Selection.extendSelection(getText(), index); 104 } 105 106 @Override 107 public void setEllipsize(TextUtils.TruncateAt ellipsis) { 108 if (ellipsis == TextUtils.TruncateAt.MARQUEE) { 109 throw new IllegalArgumentException("EditText cannot use the ellipsize mode " 110 + "TextUtils.TruncateAt.MARQUEE"); 111 } 112 super.setEllipsize(ellipsis); 113 } 114} 115