微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android,动态地从EditText更改下划线颜色

是否可以动态地从EditText更改下划线的颜色?
就像效果一样,当我专注它时,它会变成蓝色.

希望你明白我想做什么.

解决方法

创建自己的自定义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,mPaint); //draw underline

    canvas.drawLine(8,8,heightMsSize-20,mPaint); //draw left corner

    canvas.drawLine(widthMsSize-8,widthMsSize-8,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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐