带多行下划线的EditText

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值