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