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]
代码:
效果图:
[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);
}
}
推荐阅读
-
android自定义带下划线EditText
-
Android如何自定义EditText下划线?
-
Android如何自定义EditText光标与下划线颜色详解
-
Android 自定义EditText输入框带清空按钮
-
Android 自定义EditText输入框带清空按钮
-
Android实现自定义带删除功能的EditText实例
-
Android如何自定义EditText光标与下划线颜色详解
-
Android中自定义的dialog中的EditText无法弹出输入法解决方案
-
Android更改EditText下划线的颜色样式和动态获取输入的字数的代码教程
-
DownEditTextView【自定义Edittext对Android 软键盘向下的监听】