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
17package android.widget;
18
19import android.content.Context;
20import 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;
26import android.util.AttributeSet;
27import android.view.accessibility.AccessibilityEvent;
28import android.view.accessibility.AccessibilityNodeInfo;
29
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.
40 *
41 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a>
42 * guide.</p>
43 * <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 */
50public class EditText extends TextView {
51    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    }
119
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    }
131}
132