带多行下划线的EditText

来源:互联网 发布:免费网络空间 编辑:程序博客网 时间:2024/06/05 03:46

Java代码:

public class LineEditText extends EditText {    private Paint mPaint;    private Rect rect;    private static final int MARGIN = 10;    public LineEditText(Context context) {        super(context);        mPaint = new Paint();        rect = new Rect();    }    public LineEditText(Context context, AttributeSet attrs) {        super(context, attrs);        mPaint = new Paint();        rect = new Rect();        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LineEditText);//        int color = array.getColor(R.styleable.LineEditText_lineColor,0xFF80ADB7);        int color = array.getColor(R.styleable.LineEditText_lineColor,0xFFf9f9f9);        float lineWidth = array.getDimension(R.styleable.LineEditText_lineWidth,1);        mPaint.setColor(color);        mPaint.setStrokeWidth(lineWidth);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Rect r = rect;        Paint paint = mPaint;        Layout layout = getLayout();        if (!canvas.getClipBounds(r)) {            return;        }        float startX = r.left + MARGIN, stopX = r.right - MARGIN;        int count = layout.getLineCount();        float size = this.getTextSize();        int lineHeight = getLineHeight();        int height = getHeight() - getPaddingBottom() - getPaddingTop();        int n = height % lineHeight == 0 ? height / lineHeight : height / lineHeight + 1;        if (count < n) {            count = n;        }        //float pt = this.getLineSpacingExtra() /2;        float pt = size / 6;        for (int i = 1; i <= count; i++) {            int y = (int) (lineHeight * i + pt);            canvas.drawLine(startX, y , stopX, y, paint);        }    }}

布局文件:

 <com.example.view.LineEditText                    android:id="@+id/editText"                    android:layout_width="match_parent"                    android:layout_height="250dp"                    android:layout_marginLeft="50dp"                    android:layout_marginRight="50dp"                    android:layout_marginTop="80dp"                    android:background="@null"                    android:gravity="top|left"                    android:hint="欢迎提任何意见和建议..."                    android:lineSpacingExtra="18dp"                    android:lines="6"                    android:paddingLeft="10dp"                    android:paddingRight="10dp"                    android:paddingTop="10dp"                    android:textColor="#097A69"                    android:textColorHint="#71E3CF"                    android:textCursorDrawable="@drawable/cursor_white"                    android:textSize="18sp" />
cursor_white.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    android:shape="rectangle" >    <size        android:width="2dip" />    <solid        android:color="@color/white_f9" />    <padding        android:bottom="-16sp" /></shape>
外加的属性attr.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="LineEditText">        <attr name="lineColor" format="color"/>        <attr name="lineWidth" format="dimension"/>    </declare-styleable></resources>






0 0
原创粉丝点击