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 
19 import android.content.Context;
20 import android.os.Bundle;
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;
27 import android.util.AttributeSet;
28 import android.view.accessibility.AccessibilityEvent;
29 import android.view.accessibility.AccessibilityNodeInfo;
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.
41  *
42  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a>
43  * guide.</p>
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  */
51 public class EditText extends TextView {
EditText(Context context)52     public EditText(Context context) {
53         this(context, null);
54     }
55 
EditText(Context context, AttributeSet attrs)56     public EditText(Context context, AttributeSet attrs) {
57         this(context, attrs, com.android.internal.R.attr.editTextStyle);
58     }
59 
EditText(Context context, AttributeSet attrs, int defStyleAttr)60     public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
61         this(context, attrs, defStyleAttr, 0);
62     }
63 
EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)64     public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
65         super(context, attrs, defStyleAttr, defStyleRes);
66     }
67 
68     @Override
getDefaultEditable()69     protected boolean getDefaultEditable() {
70         return true;
71     }
72 
73     @Override
getDefaultMovementMethod()74     protected MovementMethod getDefaultMovementMethod() {
75         return ArrowKeyMovementMethod.getInstance();
76     }
77 
78     @Override
getText()79     public Editable getText() {
80         return (Editable) super.getText();
81     }
82 
83     @Override
setText(CharSequence text, BufferType type)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      */
setSelection(int start, int stop)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      */
setSelection(int index)98     public void setSelection(int index) {
99         Selection.setSelection(getText(), index);
100     }
101 
102     /**
103      * Convenience for {@link Selection#selectAll}.
104      */
selectAll()105     public void selectAll() {
106         Selection.selectAll(getText());
107     }
108 
109     /**
110      * Convenience for {@link Selection#extendSelection}.
111      */
extendSelection(int index)112     public void extendSelection(int index) {
113         Selection.extendSelection(getText(), index);
114     }
115 
116     @Override
setEllipsize(TextUtils.TruncateAt ellipsis)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     }
124 
125     @Override
onInitializeAccessibilityEvent(AccessibilityEvent event)126     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
127         super.onInitializeAccessibilityEvent(event);
128         event.setClassName(EditText.class.getName());
129     }
130 
131     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)132     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
133         super.onInitializeAccessibilityNodeInfo(info);
134         info.setClassName(EditText.class.getName());
135     }
136 
137     @Override
performAccessibilityAction(int action, Bundle arguments)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     }
154 }
155