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