parent
949bf858dc
commit
dd41a0a498
|
@ -34,14 +34,14 @@ public class ChameleonAutoCompleteTextView extends AppCompatAutoCompleteTextView
|
|||
@Nullable
|
||||
@Override
|
||||
public ChameleonEditText.Appearance createAppearance(@NonNull Context context, @NonNull AttributeSet attributeSet, @NonNull Chameleon.Theme theme) {
|
||||
return ChameleonEditText.Appearance.create(context, attributeSet, theme);
|
||||
return ChameleonTextView.Appearance.create(context, attributeSet, theme);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void applyAppearance(@NonNull Appearance appearance) {
|
||||
final ChameleonEditText.Appearance a = (ChameleonEditText.Appearance) appearance;
|
||||
ChameleonEditText.Appearance.apply(this, a);
|
||||
ChameleonTextView.Appearance.apply(this, a);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,24 +1,13 @@
|
|||
package org.mariotaku.chameleon.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.ColorInt;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.ViewCompat;
|
||||
import android.support.v7.widget.AppCompatEditText;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.mariotaku.chameleon.Chameleon;
|
||||
import org.mariotaku.chameleon.ChameleonUtils;
|
||||
import org.mariotaku.chameleon.ChameleonView;
|
||||
import org.mariotaku.chameleon.R;
|
||||
import org.mariotaku.chameleon.internal.ChameleonTypedArray;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 2016/12/18.
|
||||
|
@ -45,64 +34,14 @@ public class ChameleonEditText extends AppCompatEditText implements ChameleonVie
|
|||
@Nullable
|
||||
@Override
|
||||
public Appearance createAppearance(@NonNull Context context, @NonNull AttributeSet attributeSet, @NonNull Chameleon.Theme theme) {
|
||||
return Appearance.create(context, attributeSet, theme);
|
||||
return ChameleonTextView.Appearance.create(context, attributeSet, theme);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void applyAppearance(@NonNull ChameleonView.Appearance appearance) {
|
||||
final Appearance a = (Appearance) appearance;
|
||||
Appearance.apply(this, a);
|
||||
ChameleonTextView.Appearance.apply(this, a);
|
||||
}
|
||||
|
||||
public static class Appearance extends ChameleonTextView.Appearance {
|
||||
private int backgroundColor;
|
||||
|
||||
public int getBackgroundColor() {
|
||||
return backgroundColor;
|
||||
}
|
||||
|
||||
public void setBackgroundColor(int backgroundColor) {
|
||||
this.backgroundColor = backgroundColor;
|
||||
}
|
||||
|
||||
public static void apply(TextView view, Appearance appearance) {
|
||||
view.setLinkTextColor(appearance.getLinkTextColor());
|
||||
ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(appearance.getBackgroundColor()));
|
||||
setCursorTint(view, appearance.getBackgroundColor());
|
||||
}
|
||||
|
||||
public static Appearance create(Context context, AttributeSet attributeSet, Chameleon.Theme theme) {
|
||||
Appearance appearance = new Appearance();
|
||||
ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attributeSet,
|
||||
R.styleable.ChameleonEditText, theme);
|
||||
appearance.setLinkTextColor(a.getColor(R.styleable.ChameleonEditText_android_textColorLink, theme.getTextColorLink()));
|
||||
appearance.setBackgroundColor(a.getColor(R.styleable.ChameleonEditText_backgroundTint, theme.getColorAccent()));
|
||||
a.recycle();
|
||||
return appearance;
|
||||
}
|
||||
|
||||
|
||||
public static void setCursorTint(@NonNull TextView textView, @ColorInt int color) {
|
||||
try {
|
||||
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
|
||||
fCursorDrawableRes.setAccessible(true);
|
||||
int mCursorDrawableRes = fCursorDrawableRes.getInt(textView);
|
||||
Field fEditor = TextView.class.getDeclaredField("mEditor");
|
||||
fEditor.setAccessible(true);
|
||||
Object editor = fEditor.get(textView);
|
||||
Class<?> clazz = editor.getClass();
|
||||
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
|
||||
fCursorDrawable.setAccessible(true);
|
||||
Drawable[] drawables = new Drawable[2];
|
||||
drawables[0] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes);
|
||||
drawables[0] = ChameleonUtils.createTintedDrawable(drawables[0], color);
|
||||
drawables[1] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes);
|
||||
drawables[1] = ChameleonUtils.createTintedDrawable(drawables[1], color);
|
||||
fCursorDrawable.set(editor, drawables);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,14 +34,14 @@ public class ChameleonMultiAutoCompleteTextView extends AppCompatMultiAutoComple
|
|||
@Nullable
|
||||
@Override
|
||||
public ChameleonEditText.Appearance createAppearance(@NonNull Context context, @NonNull AttributeSet attributeSet, @NonNull Chameleon.Theme theme) {
|
||||
return ChameleonEditText.Appearance.create(context, attributeSet, theme);
|
||||
return ChameleonTextView.Appearance.create(context, attributeSet, theme);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void applyAppearance(@NonNull ChameleonView.Appearance appearance) {
|
||||
final ChameleonEditText.Appearance a = (ChameleonEditText.Appearance) appearance;
|
||||
ChameleonEditText.Appearance.apply(this, a);
|
||||
ChameleonTextView.Appearance.apply(this, a);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,16 +1,25 @@
|
|||
package org.mariotaku.chameleon.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.ColorInt;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.ViewCompat;
|
||||
import android.support.v7.widget.AppCompatTextView;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.mariotaku.chameleon.Chameleon;
|
||||
import org.mariotaku.chameleon.ChameleonUtils;
|
||||
import org.mariotaku.chameleon.ChameleonView;
|
||||
import org.mariotaku.chameleon.R;
|
||||
import org.mariotaku.chameleon.internal.ChameleonTypedArray;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 2016/12/18.
|
||||
*/
|
||||
|
@ -53,6 +62,76 @@ public class ChameleonTextView extends AppCompatTextView implements ChameleonVie
|
|||
|
||||
public static class Appearance implements ChameleonView.Appearance {
|
||||
private int linkTextColor;
|
||||
private int backgroundColor;
|
||||
|
||||
public static void apply(TextView view, ChameleonEditText.Appearance appearance) {
|
||||
view.setLinkTextColor(appearance.getLinkTextColor());
|
||||
ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(appearance.getBackgroundColor()));
|
||||
setCursorTint(view, appearance.getBackgroundColor());
|
||||
setHandlerTint(view, appearance.getBackgroundColor());
|
||||
view.setHighlightColor(ChameleonUtils.adjustAlpha(appearance.getBackgroundColor(), 0.4f));
|
||||
}
|
||||
|
||||
public static ChameleonEditText.Appearance create(Context context, AttributeSet attributeSet, Chameleon.Theme theme) {
|
||||
ChameleonEditText.Appearance appearance = new ChameleonEditText.Appearance();
|
||||
ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attributeSet,
|
||||
R.styleable.ChameleonEditText, theme);
|
||||
appearance.setLinkTextColor(a.getColor(R.styleable.ChameleonEditText_android_textColorLink, theme.getTextColorLink()));
|
||||
appearance.setBackgroundColor(a.getColor(R.styleable.ChameleonEditText_backgroundTint, theme.getColorAccent()));
|
||||
a.recycle();
|
||||
return appearance;
|
||||
}
|
||||
|
||||
public static void setCursorTint(@NonNull TextView textView, @ColorInt int color) {
|
||||
try {
|
||||
int mCursorDrawableRes = getIntField(TextView.class, textView, "mCursorDrawableRes");
|
||||
Object editor = getField(TextView.class, textView, "mEditor");
|
||||
Drawable[] drawables = new Drawable[2];
|
||||
drawables[0] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes);
|
||||
drawables[0] = ChameleonUtils.createTintedDrawable(drawables[0], color);
|
||||
drawables[1] = ContextCompat.getDrawable(textView.getContext(), mCursorDrawableRes);
|
||||
drawables[1] = ChameleonUtils.createTintedDrawable(drawables[1], color);
|
||||
setField(Class.forName("android.widget.Editor"), editor, "mCursorDrawable", drawables);
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setHandlerTint(@NonNull TextView textView, @ColorInt int color) {
|
||||
try {
|
||||
int mTextSelectHandleLeftRes = getIntField(TextView.class, textView, "mTextSelectHandleLeftRes");
|
||||
int mTextSelectHandleRightRes = getIntField(TextView.class, textView, "mTextSelectHandleRightRes");
|
||||
int mTextSelectHandleRes = getIntField(TextView.class, textView, "mTextSelectHandleRes");
|
||||
Object editor = getField(TextView.class, textView, "mEditor");
|
||||
final Class<?> editorClass = Class.forName("android.widget.Editor");
|
||||
setField(editorClass, editor, "mSelectHandleLeft", ChameleonUtils.createTintedDrawable(ContextCompat.getDrawable(textView.getContext(), mTextSelectHandleLeftRes), color));
|
||||
setField(Class.forName("android.widget.Editor"), editor, "mSelectHandleRight", ChameleonUtils.createTintedDrawable(ContextCompat.getDrawable(textView.getContext(), mTextSelectHandleRightRes), color));
|
||||
setField(Class.forName("android.widget.Editor"), editor, "mSelectHandleCenter", ChameleonUtils.createTintedDrawable(ContextCompat.getDrawable(textView.getContext(), mTextSelectHandleRes), color));
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Object getField(@NonNull Class<?> cls, @NonNull Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException {
|
||||
Field field = cls.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return field.get(object);
|
||||
}
|
||||
|
||||
private static int getIntField(@NonNull Class<?> cls, @NonNull Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException {
|
||||
Field field = cls.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return field.getInt(object);
|
||||
}
|
||||
|
||||
private static void setField(@NonNull Class<?> cls, @NonNull Object object, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
|
||||
Field field = cls.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
field.set(object, value);
|
||||
}
|
||||
|
||||
public int getLinkTextColor() {
|
||||
return linkTextColor;
|
||||
|
@ -61,5 +140,13 @@ public class ChameleonTextView extends AppCompatTextView implements ChameleonVie
|
|||
public void setLinkTextColor(int linkTextColor) {
|
||||
this.linkTextColor = linkTextColor;
|
||||
}
|
||||
|
||||
public int getBackgroundColor() {
|
||||
return backgroundColor;
|
||||
}
|
||||
|
||||
public void setBackgroundColor(int backgroundColor) {
|
||||
this.backgroundColor = backgroundColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue