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.AccessibilityEvent; |
| 28 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 40 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 41 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a> |
| 42 | * guide.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
Philip Milne | ab104ba | 2013-04-19 03:53:38 +0000 | [diff] [blame] | 50 | public class EditText extends TextView { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | public EditText(Context context) { |
| 52 | this(context, null); |
| 53 | } |
| 54 | |
| 55 | public EditText(Context context, AttributeSet attrs) { |
| 56 | this(context, attrs, com.android.internal.R.attr.editTextStyle); |
| 57 | } |
| 58 | |
| 59 | public EditText(Context context, AttributeSet attrs, int defStyle) { |
| 60 | super(context, attrs, defStyle); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected boolean getDefaultEditable() { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected MovementMethod getDefaultMovementMethod() { |
| 70 | return ArrowKeyMovementMethod.getInstance(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Editable getText() { |
| 75 | return (Editable) super.getText(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void setText(CharSequence text, BufferType type) { |
| 80 | super.setText(text, BufferType.EDITABLE); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Convenience for {@link Selection#setSelection(Spannable, int, int)}. |
| 85 | */ |
| 86 | public void setSelection(int start, int stop) { |
| 87 | Selection.setSelection(getText(), start, stop); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Convenience for {@link Selection#setSelection(Spannable, int)}. |
| 92 | */ |
| 93 | public void setSelection(int index) { |
| 94 | Selection.setSelection(getText(), index); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Convenience for {@link Selection#selectAll}. |
| 99 | */ |
| 100 | public void selectAll() { |
| 101 | Selection.selectAll(getText()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Convenience for {@link Selection#extendSelection}. |
| 106 | */ |
| 107 | public void extendSelection(int index) { |
| 108 | Selection.extendSelection(getText(), index); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void setEllipsize(TextUtils.TruncateAt ellipsis) { |
| 113 | if (ellipsis == TextUtils.TruncateAt.MARQUEE) { |
| 114 | throw new IllegalArgumentException("EditText cannot use the ellipsize mode " |
| 115 | + "TextUtils.TruncateAt.MARQUEE"); |
| 116 | } |
| 117 | super.setEllipsize(ellipsis); |
| 118 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 119 | |
| 120 | @Override |
| 121 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 122 | super.onInitializeAccessibilityEvent(event); |
| 123 | event.setClassName(EditText.class.getName()); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 128 | super.onInitializeAccessibilityNodeInfo(info); |
| 129 | info.setClassName(EditText.class.getName()); |
| 130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |