Android自定义edittext,带有像记事本一样的线条设计

时间:2016-12-08 14:05:49

标签: android android-edittext

我正在尝试使用记事本中的行创建自定义edittext。我遇到的最大问题是它打破了界限并且没有正确地绘制它。

    public class NoteEditText extends EditText {
        private final int heightpixels;
        private Paint mPaint;
        private Rect mRect;
        private Canvas canvas;


        public NoteEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.NoteEditText,
                    0, 0);

            this.heightpixels = getContext().getResources().getDisplayMetrics().heightPixels;
            this.mRect = new Rect();
            this.mPaint = new Paint();
            this.mPaint.setStyle(Paint.Style.STROKE);
            try {
                mPaint.setColor(Color.parseColor(a.getString(R.styleable.NoteEditText_lineColor)));
            } catch (NullPointerException e) {
                mPaint.setColor(Color.parseColor("#ffffff"));
            }
        }

        public NoteEditText(Context context) {
            super(context);
            this.heightpixels = getContext().getResources().getDisplayMetrics().heightPixels;
            this.mRect = new Rect();
            this.mPaint = new Paint();
            this.mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.parseColor("#ffffff"));
        }

        protected void onDraw(Canvas canvas) {
            int count;
            this.canvas = canvas;
            int lineheight = getLineHeight();
            if (TextUtils.isEmpty(getText())) {
                count = (this.heightpixels / lineheight) + 2;
            } else {
                count = getLineCount();
            }
            Rect r = this.mRect;
            Paint paint = this.mPaint;
            int baseline = getLineBounds(0, r);
            for (int i = 0; i < count; i++) {
                canvas.drawLine((float) r.left, (float) (baseline + 1), (float) r.right, (float) (baseline + 1), paint);
                baseline += lineheight;
            }
            super.onDraw(canvas);
        }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }

    @Override
    public void onDrawForeground(Canvas canvas) {
        super.onDrawForeground(canvas);
    }
}

当您尝试在新行中添加文本并尝试删除时,通常会发生这种情况。 example

这也是XML

<com.example.dev.customedittext.NoteEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:ems="10"
    android:gravity="top"
    android:textColor="@android:color/black"
    android:textColorHint="#AAAAAA"
    android:textSize="12sp"
    app:lineColor="#000000"
    />

是否有任何建议如何解决此问题?

1 个答案:

答案 0 :(得分:1)

使用此:

<com.testing.remon.LineEditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="@null"
android:inputType="textMultiLine|textNoSuggestions"
android:minLines="10"
android:singleLine="false"
android:imeOptions="actionNone"
/>