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; |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame] | 20 | import android.graphics.Rect; |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame] | 28 | import android.util.ValueModel; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 29 | import android.view.accessibility.AccessibilityEvent; |
| 30 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* |
| 34 | * This is supposed to be a *very* thin veneer over TextView. |
| 35 | * Do not make any changes here that do anything that a TextView |
| 36 | * with a key listener and a movement method wouldn't do! |
| 37 | */ |
| 38 | |
| 39 | /** |
| 40 | * EditText is a thin veneer over TextView that configures itself |
| 41 | * to be editable. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 42 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 43 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a> |
| 44 | * guide.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | * <p> |
| 46 | * <b>XML attributes</b> |
| 47 | * <p> |
| 48 | * See {@link android.R.styleable#EditText EditText Attributes}, |
| 49 | * {@link android.R.styleable#TextView TextView Attributes}, |
| 50 | * {@link android.R.styleable#View View Attributes} |
| 51 | */ |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame] | 52 | public class EditText extends TextView implements ValueEditor<CharSequence> { |
| 53 | private ValueModel<CharSequence> mValueModel = ValueModel.EMPTY; |
| 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | public EditText(Context context) { |
| 56 | this(context, null); |
| 57 | } |
| 58 | |
| 59 | public EditText(Context context, AttributeSet attrs) { |
| 60 | this(context, attrs, com.android.internal.R.attr.editTextStyle); |
| 61 | } |
| 62 | |
| 63 | public EditText(Context context, AttributeSet attrs, int defStyle) { |
| 64 | super(context, attrs, defStyle); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected boolean getDefaultEditable() { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected MovementMethod getDefaultMovementMethod() { |
| 74 | return ArrowKeyMovementMethod.getInstance(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Editable getText() { |
| 79 | return (Editable) super.getText(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void setText(CharSequence text, BufferType type) { |
| 84 | super.setText(text, BufferType.EDITABLE); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Convenience for {@link Selection#setSelection(Spannable, int, int)}. |
| 89 | */ |
| 90 | public void setSelection(int start, int stop) { |
| 91 | Selection.setSelection(getText(), start, stop); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Convenience for {@link Selection#setSelection(Spannable, int)}. |
| 96 | */ |
| 97 | public void setSelection(int index) { |
| 98 | Selection.setSelection(getText(), index); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Convenience for {@link Selection#selectAll}. |
| 103 | */ |
| 104 | public void selectAll() { |
| 105 | Selection.selectAll(getText()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Convenience for {@link Selection#extendSelection}. |
| 110 | */ |
| 111 | public void extendSelection(int index) { |
| 112 | Selection.extendSelection(getText(), index); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void setEllipsize(TextUtils.TruncateAt ellipsis) { |
| 117 | if (ellipsis == TextUtils.TruncateAt.MARQUEE) { |
| 118 | throw new IllegalArgumentException("EditText cannot use the ellipsize mode " |
| 119 | + "TextUtils.TruncateAt.MARQUEE"); |
| 120 | } |
| 121 | super.setEllipsize(ellipsis); |
| 122 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 123 | |
| 124 | @Override |
| 125 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 126 | super.onInitializeAccessibilityEvent(event); |
| 127 | event.setClassName(EditText.class.getName()); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 132 | super.onInitializeAccessibilityNodeInfo(info); |
| 133 | info.setClassName(EditText.class.getName()); |
| 134 | } |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame] | 135 | |
| 136 | @Override |
| 137 | public ValueModel<CharSequence> getValueModel() { |
| 138 | return mValueModel; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void setValueModel(ValueModel<CharSequence> valueModel) { |
| 143 | mValueModel = valueModel; |
| 144 | setText(mValueModel.get()); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | void sendAfterTextChanged(Editable text) { |
| 149 | super.sendAfterTextChanged(text); |
| 150 | mValueModel.set(text); |
| 151 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |