blob: ad355504a0ee652d8e3a4519e6bf390fc268a61f [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.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
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 Main41ec6532010-08-19 16:57:07 -070040 *
Scott Main4c359b72012-07-24 15:51:27 -070041 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a>
42 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 * <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 Milneab104ba2013-04-19 03:53:38 +000050public class EditText extends TextView {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 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
Alan Viverette617feb92013-09-09 18:09:13 -070059 public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
60 this(context, attrs, defStyleAttr, 0);
61 }
62
63 public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
64 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
66
67 @Override
Siyamed Sinir02771192016-02-05 16:08:59 -080068 public boolean getFreezesText() {
69 return true;
70 }
71
72 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 protected boolean getDefaultEditable() {
74 return true;
75 }
76
77 @Override
78 protected MovementMethod getDefaultMovementMethod() {
79 return ArrowKeyMovementMethod.getInstance();
80 }
81
82 @Override
83 public Editable getText() {
84 return (Editable) super.getText();
85 }
86
87 @Override
88 public void setText(CharSequence text, BufferType type) {
89 super.setText(text, BufferType.EDITABLE);
90 }
91
92 /**
93 * Convenience for {@link Selection#setSelection(Spannable, int, int)}.
94 */
95 public void setSelection(int start, int stop) {
96 Selection.setSelection(getText(), start, stop);
97 }
98
99 /**
100 * Convenience for {@link Selection#setSelection(Spannable, int)}.
101 */
102 public void setSelection(int index) {
103 Selection.setSelection(getText(), index);
104 }
105
106 /**
107 * Convenience for {@link Selection#selectAll}.
108 */
109 public void selectAll() {
110 Selection.selectAll(getText());
111 }
112
113 /**
114 * Convenience for {@link Selection#extendSelection}.
115 */
116 public void extendSelection(int index) {
117 Selection.extendSelection(getText(), index);
118 }
119
Siyamed Sinir192d3bd2016-03-02 14:11:34 -0800120 /**
121 * Causes words in the text that are longer than the view's width to be ellipsized instead of
122 * broken in the middle. {@link TextUtils.TruncateAt#MARQUEE
123 * TextUtils.TruncateAt#MARQUEE} is not supported.
124 *
125 * @param ellipsis Type of ellipsis to be applied.
126 * @throws IllegalArgumentException When the value of <code>ellipsis</code> parameter is
127 * {@link TextUtils.TruncateAt#MARQUEE}.
128 * @see TextView#setEllipsize(TextUtils.TruncateAt)
129 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 @Override
131 public void setEllipsize(TextUtils.TruncateAt ellipsis) {
132 if (ellipsis == TextUtils.TruncateAt.MARQUEE) {
133 throw new IllegalArgumentException("EditText cannot use the ellipsize mode "
134 + "TextUtils.TruncateAt.MARQUEE");
135 }
136 super.setEllipsize(ellipsis);
137 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800138
139 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800140 public CharSequence getAccessibilityClassName() {
141 return EditText.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800142 }
Guang Zhu4cd353c2014-02-12 19:54:30 -0800143
Alan Viverettea54956a2015-01-07 16:05:02 -0800144 /** @hide */
Guang Zhu4cd353c2014-02-12 19:54:30 -0800145 @Override
Svetoslavf4b1ada2015-11-09 11:57:14 -0800146 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
147 super.onInitializeAccessibilityNodeInfoInternal(info);
148 if (isEnabled()) {
149 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_TEXT);
150 }
151 }
152
153 /** @hide */
154 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800155 public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Guang Zhu4cd353c2014-02-12 19:54:30 -0800156 switch (action) {
157 case AccessibilityNodeInfo.ACTION_SET_TEXT: {
Phil Weaver44b3b912016-03-16 18:20:16 -0700158 if (!isEnabled()) {
159 return false;
160 }
Guang Zhu4cd353c2014-02-12 19:54:30 -0800161 CharSequence text = (arguments != null) ? arguments.getCharSequence(
162 AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE) : null;
163 setText(text);
164 if (text != null && text.length() > 0) {
165 setSelection(text.length());
166 }
167 return true;
168 }
169 default: {
Alan Viverettea54956a2015-01-07 16:05:02 -0800170 return super.performAccessibilityActionInternal(action, arguments);
Guang Zhu4cd353c2014-02-12 19:54:30 -0800171 }
172 }
173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174}