安卓笔记本设计多行下划线Edittext


首先贴控件CustomEditText代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

/**
 * @author Linmiansheng
 */
public class CustomEditText extends android.support.v7.widget.AppCompatEditText {

    //	private static final String TAG = "com.lms.todo.views.CustomEditText";
    private Rect mRect;
    private Paint mPaint;

    private final int padding = 10;

    private int lineHeight;
    private int viewHeight, viewWidth;

    public CustomEditText(Context context) {
        this(context, null);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);

        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;
        int lineHeight = 0;
        int i = 0;
        while (i < count) {
            lineHeight = getLineBounds(i, r);
            canvas.drawLine(r.left, lineHeight + padding, r.right, lineHeight + padding,
                    paint);
            i++;
        }
        int maxLines = 15;
        int avgHeight = lineHeight / count;
        int currentLineHeight = lineHeight;

        while (i < maxLines) {
            currentLineHeight = currentLineHeight + avgHeight + padding;
            canvas.drawLine(r.left, currentLineHeight, r.right, currentLineHeight, paint);
            i++;
        }
        super.onDraw(canvas);
    }
}
对行高padding进行调整 然后贴XML调用代码
<com.sjm.suchbear.notepad.View.CustomEditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="@dimen/dimen_5_dp"
            android:textColor="@color/black"
            android:inputType="textMultiLine|textNoSuggestions"
            android:gravity="top"
            android:textSize="@dimen/dimen_14_dp" />
其中设置了lineSpacingExtra 行间距属性才能显示最后一行的下划线 inputType属性中 textMultiLine确保多行自动换行功能 textNoSuggestions 让英文文本不接受拼写检查 完毕!