blob: 57e51c259251ee2371e2d4fd137e3bb95859914c [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;
Gilles Debunnef5c6eff2010-02-09 19:08:36 -080020import android.text.Editable;
21import android.text.Selection;
22import android.text.Spannable;
23import android.text.TextUtils;
24import android.text.method.ArrowKeyMovementMethod;
25import android.text.method.MovementMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.AttributeSet;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080027import android.view.accessibility.AccessibilityEvent;
28import 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
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 Ganov8a78fd42012-01-17 14:36:46 -0800119
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 Project9066cfe2009-03-03 19:31:44 -0800131}