在多行edittext中创建水平线

时间:2014-10-15 17:30:24

标签: android

我想在edittext中创建水平线。我在网上找了这个课。如何在edittext et1所在的活动中调用此类? 感谢

public class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;

// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
}

}

1 个答案:

答案 0 :(得分:0)

您需要将EditText替换为该类的自定义视图,即LinedEditText。您没有修改现有的EditText,而是使用扩展EditText类的类的新实例替换它。

如果您在xml文件中定义了EditText,则首先需要将其替换为新文件。要执行此操作,请将其更改为使用<com.example.customviews.LinedEditText将第一部分替换为您的包名称。

在Java部分中,只需将实例更改为LinedEditText并将其更改为(LinedEditText)findViewById(R.id.edit1)