设置边框的宽度为2,当文本框获取焦点时和没获取焦点的边框颜色不一样
canvas.drawRect(...)是一个长方形
canvas.drawRoundRect(...)是一个带圆弧的长方形
package com.newedittext.ui;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.widget.EditText;
public class NEditText extends EditText {
public NEditText(Context context) {
super(context);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
if(this.isFocused() == true){
paint.setColor(Color.parseColor("#122e29"));
} else {
paint.setColor(Color.rgb(0,173,173));
}
canvas.drawRect(new Rect(3+this.getScrollX(), 3+this.getScrollY(), this.getWidth()-3+this.getScrollX(), this.getHeight()+ this.getScrollY()-3), paint);
//canvas.drawRoundRect(new RectF(2+this.getScrollX(), 2+this.getScrollY(), this.getWidth()-3+this.getScrollX(), this.getHeight()+ this.getScrollY()-1), 3,3, paint);
super.onDraw(canvas);
}
}