Java tutorial
/* * Copyright (C) 2015 Steven Lewi * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fenlisproject.elf.core.widget; /* * This is supposed to be a *very* thin veneer over TextView. * Do not make any changes here that do anything that a TextView * with a key listener and a movement method wouldn't do! */ import android.annotation.TargetApi; import android.content.Context; import android.content.res.ColorStateList; import android.graphics.PorterDuff; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.view.TintableBackgroundView; import android.support.v7.internal.widget.TintContextWrapper; import android.support.v7.internal.widget.TintInfo; import android.support.v7.internal.widget.TintManager; import android.support.v7.internal.widget.TintTypedArray; import android.text.Editable; import android.text.Selection; import android.text.Spannable; import android.text.TextUtils; import android.text.method.ArrowKeyMovementMethod; import android.text.method.MovementMethod; import android.util.AttributeSet; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; import android.widget.EditText; /** * EditText is a thin veneer over TextView that configures itself * to be editable. * <p/> * <p>See the <a href="{@docRoot}guide/topics/ui/controls/text.html">Text Fields</a> * guide.</p> * <p/> * <b>XML attributes</b> * <p/> * See {@link android.R.styleable#EditText EditText Attributes}, * {@link android.R.styleable#TextView TextView Attributes}, * {@link android.R.styleable#View View Attributes} */ public class ExtendedEditText extends ExtendedTextView implements TintableBackgroundView { private static final int[] TINT_ATTRS = { android.R.attr.background }; private TintInfo mBackgroundTint; public ExtendedEditText(Context context) { this(context, null); } public ExtendedEditText(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); } public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(TintContextWrapper.wrap(context), attrs, defStyleAttr); if (TintManager.SHOULD_BE_USED) { TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, TINT_ATTRS, defStyleAttr, 0); if (a.hasValue(0)) { ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1)); if (tint != null) { setSupportBackgroundTintList(tint); } } a.recycle(); } } /** * This should be accessed via * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, * android.content.res.ColorStateList)} * * @hide */ @Override public void setSupportBackgroundTintList(@Nullable ColorStateList tint) { if (mBackgroundTint == null) { mBackgroundTint = new TintInfo(); } mBackgroundTint.mTintList = tint; mBackgroundTint.mHasTintList = true; applySupportBackgroundTint(); } /** * This should be accessed via * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)} * * @hide */ @Nullable @Override public ColorStateList getSupportBackgroundTintList() { return mBackgroundTint != null ? mBackgroundTint.mTintList : null; } /** * This should be accessed via * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, android.graphics.PorterDuff.Mode)} * * @hide */ @Override public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { if (mBackgroundTint == null) { mBackgroundTint = new TintInfo(); } mBackgroundTint.mTintMode = tintMode; mBackgroundTint.mHasTintMode = true; applySupportBackgroundTint(); } /** * This should be accessed via * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)} * * @hide */ @Override public PorterDuff.Mode getSupportBackgroundTintMode() { return mBackgroundTint != null ? mBackgroundTint.mTintMode : null; } @Override protected void drawableStateChanged() { super.drawableStateChanged(); applySupportBackgroundTint(); } private void applySupportBackgroundTint() { if (getBackground() != null && mBackgroundTint != null) { TintManager.tintViewBackground(this, mBackgroundTint); } } @Override protected boolean getDefaultEditable() { return true; } @Override protected MovementMethod getDefaultMovementMethod() { return ArrowKeyMovementMethod.getInstance(); } @Override public Editable getText() { return (Editable) super.getText(); } @Override public void setText(CharSequence text, BufferType type) { super.setText(text, BufferType.EDITABLE); } /** * Convenience for {@link Selection#setSelection(Spannable, int, int)}. */ public void setSelection(int start, int stop) { Selection.setSelection(getText(), start, stop); } /** * Convenience for {@link Selection#setSelection(Spannable, int)}. */ public void setSelection(int index) { Selection.setSelection(getText(), index); } /** * Convenience for {@link Selection#selectAll}. */ public void selectAll() { Selection.selectAll(getText()); } /** * Convenience for {@link Selection#extendSelection}. */ public void extendSelection(int index) { Selection.extendSelection(getText(), index); } @Override public void setEllipsize(TextUtils.TruncateAt ellipsis) { if (ellipsis == TextUtils.TruncateAt.MARQUEE) { throw new IllegalArgumentException( "EditText cannot use the ellipsize mode " + "TextUtils.TruncateAt.MARQUEE"); } super.setEllipsize(ellipsis); } @Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setClassName(EditText.class.getName()); } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.setClassName(EditText.class.getName()); } @Override public boolean performAccessibilityAction(int action, Bundle arguments) { switch (action) { case AccessibilityNodeInfo.ACTION_SET_TEXT: { CharSequence text = (arguments != null) ? arguments.getCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE) : null; setText(text); if (text != null && text.length() > 0) { setSelection(text.length()); } return true; } default: { return super.performAccessibilityAction(action, arguments); } } } }