android - 在 edittext 中绘制多行,例如记事本

标签 android android-edittext

我正在查看 android SDK 中的记事本示例,请参见此处:http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html

它只绘制光标所在的当前行,例如 http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg

但我想显示填满屏幕的行,例如http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png

任何建议都会很棒。相关的代码似乎在这里:

    protected void onDraw(Canvas canvas) {

        // Gets the number of lines of text in the View.
        int count = getLineCount();

        // Gets the global Rect and Paint objects
        Rect r = mRect;
        Paint paint = mPaint;

        /*
         * Draws one line in the rectangle for every line of text in the EditText
         */
        for (int i = 0; i < count; i++) {

            // Gets the baseline coordinates for the current line of text
            int baseline = getLineBounds(i, r);

            /*
             * Draws a line in the background from the left of the rectangle to the right,
             * at a vertical position one dip below the baseline, using the "paint" object
             * for details.
             */
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        // Finishes up by calling the parent method
        super.onDraw(canvas);
    }

最佳答案

这是代码,基于jkhouws1的建议和谷歌的note editor

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);
    }
}

在 Eclipse IDE 中按 Ctrl+Shift+O 添加所有需要的导入

关于android - 在 edittext 中绘制多行,例如记事本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5972388/

相关文章:

android - Edittext在android中可编辑和不可编辑

android - 编辑文本 : suppress soft keyboard but allow selection and cursor?

java - 如何从对话框内的edittext获取文本

android - 内存不足会导致 native 代码出现段错误吗?

android - 如何使用 Android 应用程序在 Web View 中呈现 pdf 文件?

android - EditText afterTextChanged 不工作 : displays null

android - 如何设置 UnderlineSpan 的颜色?

android - 如何在android中绘制源和目的地之间的火车路线图

android - dalvik-cache 阻止文件创建

java - 构建opencv失败