feat(EditText): 光标染色

This commit is contained in:
HuanCheng65 2023-07-20 17:59:53 +08:00
parent 63c7ad885c
commit 0a86dffd8e
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
2 changed files with 27 additions and 0 deletions

View File

@ -2,16 +2,20 @@ package com.huanchengfly.tieba.post.ui.widgets.theme;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import com.huanchengfly.tieba.post.R;
import com.huanchengfly.tieba.post.ui.common.theme.interfaces.Tintable;
import com.huanchengfly.tieba.post.ui.common.theme.utils.ColorStateListUtils;
import com.huanchengfly.tieba.post.ui.common.theme.utils.ThemeUtils;
import com.huanchengfly.tieba.post.ui.widgets.edittext.widget.UndoableEditText;
public class TintUndoableEditText extends UndoableEditText implements Tintable {
private int mTextColorResId;
private int mHintTextColorResId;
private int mCursorColorResId;
public TintUndoableEditText(Context context) {
super(context);
@ -35,12 +39,14 @@ public class TintUndoableEditText extends UndoableEditText implements Tintable {
if (attrs == null) {
mTextColorResId = 0;
mHintTextColorResId = 0;
mCursorColorResId = 0;
tint();
return;
}
TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TintUndoableEditText, defStyleAttr, 0);
mTextColorResId = array.getResourceId(R.styleable.TintUndoableEditText_textColor, 0);
mHintTextColorResId = array.getResourceId(R.styleable.TintUndoableEditText_hintTextColor, 0);
mCursorColorResId = array.getResourceId(R.styleable.TintUndoableEditText_cursorColor, 0);
array.recycle();
tint();
}
@ -53,5 +59,25 @@ public class TintUndoableEditText extends UndoableEditText implements Tintable {
if (mHintTextColorResId != 0) {
setHintTextColor(ColorStateListUtils.createColorStateList(getContext(), mHintTextColorResId));
}
if (mCursorColorResId != 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Drawable drawable;
drawable = getTextCursorDrawable();
if (drawable == null) {
return;
}
setTextCursorDrawable(ThemeUtils.tintDrawable(drawable, ColorStateListUtils.createColorStateList(getContext(), mCursorColorResId)));
}
}
}
public void setTextColorResId(int textColorResId) {
mTextColorResId = textColorResId;
tint();
}
public void setHintTextColorResId(int hintTextColorResId) {
mHintTextColorResId = hintTextColorResId;
tint();
}
}

View File

@ -150,6 +150,7 @@
</declare-styleable>
<declare-styleable name="TintUndoableEditText">
<attr name="cursorColor" format="color" />
<attr name="hintTextColor" format="color" />
<attr name="textColor" />
</declare-styleable>