blob: a8ff56280a29a8734c01243c9e0a4297df77673c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.Context;
Guang Zhu4cd353c2014-02-12 19:54:30 -080020import android.os.Bundle;
Gilles Debunnef5c6eff2010-02-09 19:08:36 -080021import android.text.Editable;
22import android.text.Selection;
23import android.text.Spannable;
24import android.text.TextUtils;
25import android.text.method.ArrowKeyMovementMethod;
26import android.text.method.MovementMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.AttributeSet;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080028import android.view.accessibility.AccessibilityEvent;
29import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
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 Main41ec6532010-08-19 16:57:07 -070041 *
Scott Main4c359b72012-07-24 15:51:27 -070042 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a>
43 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 * <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 Milneab104ba2013-04-19 03:53:38 +000051public class EditText extends TextView {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 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 Viverette617feb92013-09-09 18:09:13 -070060 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 Project9066cfe2009-03-03 19:31:44 -080066 }
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 Ganov8a78fd42012-01-17 14:36:46 -0800124
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 Zhu4cd353c2014-02-12 19:54:30 -0800136
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 Project9066cfe2009-03-03 19:31:44 -0800154}