欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android自定义带下划线EditText

程序员文章站 2023-12-23 11:23:28
...
[size=medium] [b] 最近做项目,需要用到带下划线的EditText,通过学习Android提供的示例Notepad,对其进行小小的改动,解决文字压线的问题,但是光标压线的问题没有解决,希望以后能够解决,效果和代码如下:[/b][/size]
效果图:
[img]http://dl.iteye.com/upload/attachment/0073/2710/bd104529-c13f-376e-9e3c-f1176d0d46ce.png[/img]
代码:

public class UnderlineEditText extends EditText {
private static final String TAG = "UnderlineEditText";
private Paint mPaint;
private Rect mRect;
private float mult = 1.5f;
private float add = 2.0f;

public UnderlineEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public UnderlineEditText(Context context) {
super(context);
init();
}

private void init() {
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.GRAY);
mPaint.setAntiAlias(true);
this.setLineSpacing(add, mult);
}

@Override
public void onDraw(Canvas canvas) {
Log.d(TAG, "func [onDraw]");
int count = getLineCount();
for (int i = 0; i < count; i++) {
getLineBounds(i, mRect);
int baseline = (i + 1) * getLineHeight();
canvas.drawLine(mRect.left, baseline, mRect.right, baseline, mPaint);
}
super.onDraw(canvas);
}

}

上一篇:

下一篇: