如何知道开放平台添加到Android的一个的EditText? [英] How to add pagelines to a EditText in android?

查看:100
本文介绍了如何知道开放平台添加到Android的一个的EditText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能显示一个知道开放平台的EditText

Is it possible to show pagelines in a EditText?

我的意思是这几行:

比方说我的的EditText 为500通过500像素的大小。我希望这些线由500平方米至横跨了500可见。

Let's say my EditText is 500 by 500 pixels in size. I want those lines to be visible across that 500 by 500 square.

有一个内置的方式做到这一点?我已经尝试过谷歌,但我无法找到答案。 我想我的另一种选择是基于下textHeight和linespacing动态地创建一个图形,这样一个丑陋的解决方法。

Is there a build in way to do this? I already tried Google but I couldn't find an answer. I guess my other option is to dynamically create a graphic based on the textheight and linespacing, such an ugly work-around.

推荐答案

从Android开发人员网站上的记事本应用程序示例展示了如何做到这一点。

The notepad application sample from the android dev site shows you how to do this.

<一个href="http://developer.android.com/resources/samples/NotePad/index.html">http://developer.android.com/resources/samples/NotePad/index.html

看起来这(向下滚动code):

Looks like this (scroll down for code):

大部分相关code是在<一个href="http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html">this文件。需要注意的是 LinedEditText 内部类。它是在活动中所定义。它汲取所需的线路。

Most of the relevant code is in this file. Pay attention to the LinedEditText inner class. It is defined within the activity. It draws the lines required.

在活动的onCreate()方法,的setContentView(R.id.note_editor)设置为视图,它的定义如下<一个href="http://developer.android.com/resources/samples/NotePad/res/layout/note_editor.html">here

Inside the activity onCreate() method, setContentView(R.id.note_editor) is set as the view, it is defined like here

片段从<一个提取href="http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html">here. 更新:code。通过@ Pieter888修改,以对整个的EditText 控制画线

Snippet extracted from here. Update: Code modified by @Pieter888 to draw lines on the entire EditText control.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

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

    public LinedEditText(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0xFF000000);
    }

    /**
     * This is called to draw the LinedEditText object
     * @param canvas The canvas on which the background is drawn.
     */
    @Override
    protected void onDraw(Canvas canvas) 
    {
        int height = canvas.getHeight();
        int curHeight = 0;
        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);
        for (curHeight = baseline + 1; curHeight < height; 
                                                 curHeight += getLineHeight())
        {
            canvas.drawLine(r.left, curHeight, r.right, curHeight, paint);
        }
        super.onDraw(canvas);
    }
}

这篇关于如何知道开放平台添加到Android的一个的EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆