Android,如何动态改变EditText下划线颜色?

7

能否动态地改变EditText下划线的颜色?例如,当我聚焦它时,它变成蓝色。

希望您明白我的意思。

3个回答

4

创建您自己的定制EditText控件。

这是我为您制作的示例:

当选中时,您只需将mPaint.setColor(Color.GREEN);更改为另一种颜色即可。

public class CustomEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;
    int widthMsSize;
    int heightMsSize;
    // we need this constructor for LayoutInflater
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.GREEN);
        System.out.println("constructor");
    }
    protected void onMeasure(final int widthMeasureSpec,
        final int heightMeasureSpec) {
        // Extract the Ms (MesaureSpec) parameters
        widthMsSize = MeasureSpec.getSize(widthMeasureSpec);
        heightMsSize = MeasureSpec.getSize(heightMeasureSpec);
        System.out.println("on measure");
        // Satisfy contract by calling setMeasuredDimension
        setMeasuredDimension(widthMsSize,
            heightMsSize);
    }
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(5, heightMsSize - 10, widthMsSize - 5, heightMsSize - 10, mPaint); //draw underline
        canvas.drawLine(8, heightMsSize - 10, 8, heightMsSize - 20, mPaint); //draw left corner
        canvas.drawLine(widthMsSize - 8, heightMsSize - 10, widthMsSize - 8, heightMsSize - 20, mPaint); //draw right corner
        super.onDraw(canvas);
    }
}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.testanimationfadeinfadeout.CustomEditText
        android:id="@+id/textedit"
        android:layout_width="228dp"
        android:layout_height="41dp"
        android:ems="10"
        android:hint="color is changed" />

</LinearLayout>

哇,谢谢你发布这篇快速而有用的文章! 只有一个(愚蠢的)问题。如果我想在我的项目中动态添加CustomEditText,那么应该为AttributeSet放什么? 晚上好,非常感谢并对糟糕的英语表示歉意 ;) - R.Z
再次问候!AttributeSet是您在xml中声明的属性,例如android:hint等。如果您不需要它,则必须使用public CustomEditText(Context context){super(context); ...}作为构造函数附:stackoverflow上没有愚蠢的问题! :) - Frank
你好,第三次来啦! 我现在遇到了一个新问题。我的布局上没有任何EditText。我需要覆盖一些函数,比如getDraw或者使用Layout的其他函数吗?我的代码:layout = (LinearLayout)findViewById(R.id.main_layout); CustomEditText customET = new CustomEditText(this); //getApplicationContext();也不行。 customET.setText(text); customET.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); layout.addView(customET);你有什么想法,可能是什么问题呢? - R.Z

2
public static void setEditTextUnderlineColor(final EditText editText, final int focusedColor, final int unfocusedColor) {
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                editText.getBackground().setColorFilter(focusedColor, PorterDuff.Mode.SRC_ATOP);
                return;
            }
            editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
        }
    });

    editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);

旧帖但仍然很棒。 - oga

0

要自定义编辑文本,我是按照以下方式进行的。这种方法对我来说非常简单。

public class CardNumberText extends EditText {

    boolean isFocus;
    Paint mPaint;
    Rect mRect;
    int widthSize, heightSize;

    public CardNumberText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        initStyle();

    }

    private void initStyle() {
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#B3B3B3"));
    }

    public CardNumberText(Context context, AttributeSet attrs) {
        super(context, attrs);

        initStyle();
    }

    public CardNumberText(Context context) {
        super(context);

        initStyle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = mRect;
        Paint paint = mPaint;
        if (isFocus) {
            mPaint.setStrokeWidth(3.0f);
            mPaint.setColor(Color.parseColor("#80CBC4"));
        } else {
            mPaint.setStrokeWidth(1.5f);
            mPaint.setColor(Color.parseColor("#B3B3B3"));
        }
        for (int i = 0; i < getLineCount(); i++) {
            int baseline = getLineBounds(i, rect);
            canvas.drawLine(rect.left, baseline + 20, rect.right, baseline,
                    paint);
        }

    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            isFocus = true;
        } else {
            isFocus = false;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接