今天在处理一个EditText的时候,想着把EditText做成像一本作业本上的纸一样,每一行都可以由线条隔开,具体效果如下:

1)最开始的思路

一开始的想法是很简单的,找出每一行的高度,然后一行一行地画线不就好了吗,代码如下:

viewHeight = getMeasuredHeight();
viewWidth = getMeasuredWidth();
lineHeight = getLineHeight();
int maxLines = viewHeight / lineHeight + 1;
int i = 0;
int currentLineHeight = 0;
while(i < maxLines){
currentLineHeight += lineHeight;
canvas.drawLine(0, currentLineHeight, viewWidth, currentLineHeight, mPaint);
i++;
}

但是出来的效果一看,高度很明显不够,如下:

看起来好像是高度的问题,那就每一行的高度都加一点呗,再试试:

lineHeight = getLineHeight() + 5;

再来看一下效果,

前面几行好像效果不错呀,但是到后面又挤在一起了,我估计这里面的原因应该是getLineHeight返回来的值不是标准的,而是随着内容的变化而不同,看一下它的函数说明,如下:

/**
* @return the height of one standard line in pixels. Note that markup
* within the text can cause individual lines to be taller or shorter
* than this height, and the layout may contain additional first-
* or last-line padding.
*/
public int getLineHeight() {
return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd);
}

果然是这样,那么通过这个getLineHeight来表示每一行的高度就不可行了。

在android提供的sample里面,有一个是NotePad的小demo,在里面它实现了为EditText里面的每一行添加一条下划线,其代码如下:

public static class LinedEditText extends EditText {

...
@Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
Rect r = mRect;
Paint paint = mPaint; for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, r); canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
} super.onDraw(canvas);
}
}

好,我们就用它这个来试试,看效果

不错哦!但是发现一个问题,后面没有字符的它就不划线了,这是因为它划线的基础在于getLineCount这个函数,定义如下:

/**
* Return the number of lines of text, or 0 if the internal Layout has not
* been built.
*/
public int getLineCount() {
return mLayout != null ? mLayout.getLineCount() : 0;
}

就是说,它返回来的值是跟内容有关的的,而getLineBounds的取值则是基于getLineCount的,定义如下(看第一句注释):

/**
* Return the baseline for the specified line (0...getLineCount() - 1)
* If bounds is not null, return the top, left, right, bottom extents
* of the specified line in it. If the internal Layout has not been built,
* return 0 and set bounds to (0, 0, 0, 0)
* @param line which line to examine (0..getLineCount() - 1)
* @param bounds Optional. If not null, it returns the extent of the line
* @return the Y-coordinate of the baseline
*/
public int getLineBounds(int line, Rect bounds)

所以,只有有内容的行才会画下划线,可是我们的目标是没有内容也想划一条线,如何是好呢?

我的想法一样很简单,在屏幕上显示的线条其实是有 限的,那么就分成两部分,前面一部分是有内容的,就拿上面NotePad这种办法画,如果线条的数目已填满这个区域了(其实是达到预先定义的最大数目 了),则不必自己再去画,反之,如果还差几样没有画,那么就根据前面已经画出来的平均高度,把剩下的几条线也给画上,因为当输入内容的时 候,EditText其实是一直在刷新的,所以自然而然,新添加的内容的线条也是应用上面NotePad那种方法来画的,所以画出来的线也是符合要求的。 这个自定义EditText的完整代码如下:

package com.lms.todo.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText; /**
* @author Linmiansheng
*/
public class CustomEditText extends EditText { // 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.lms.todo.views.CustomEditText
android:id="@+id/etContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="8"
android:background="#00000000"
android:gravity="top"
android:inputType="textMultiLine"
android:padding="5dip"
android:textSize="20sp" />

结束。

Android学习小Demo一个显示行线的自定义EditText的更多相关文章

  1. Android学习小Demo(19)利用Loader来实时接收短信

    之前写过一篇文章<Android学习小Demo(13)Android中关于ContentObserver的使用>,在里面利用ContentOberver去监測短信URI内容的变化.我们先来 ...

  2. Android学习笔记-构建一个可复用的自定义BaseAdapter

    转载自http://www.runoob.com/w3cnote/android-tutorial-customer-baseadapter.html   作者:coder-pig 本节引言: 如题, ...

  3. Android学习小Demo(20)关于Fragment的应用

    Android在3.0之后引入了Fragment的概念,我推測其想法可能仅仅是想更好地兼容大屏幕或者平板的开发,由于大屏幕能够展示很多其它的内容,而内容一多,逻辑有可能就乱,而利用Fragment,则 ...

  4. Android学习小Demo(21)ListView的联动选择

    在日常的App开发中,尤其是在开发生活服务的应用上,非常多时候,我们会须要联动地展现省市区的数据等,需求大概例如以下: 1)展现全部省份 2)当点击某省份的时候,在二级菜单上展现此省份以下所属的城市列 ...

  5. RPC框架学习+小Demo实例

    一.什么是RPC协议? 全称:远程过程调度协议 效果:使消费者向调用本地方法一样调用远程服务方法,对使用者透明 目前常用:Dubbo.Thirft.Sofa.... 功能: 建立远程通信(socket ...

  6. Android学习系列(3)--App自动更新之自定义进度视图和内部存储

    友好的视觉感知和稳定的不出错表现,来自于我们追求美感和考虑的全面性,博客园从技术的角度,一直我都很欣赏.这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 这 ...

  7. 高强度学习训练第二天总结:Opencv+Android+CameraView小demo

    前言:网上已经有很多人将Opencv集成进Android项目中了.因此我只给大家看Gradle文件和项目目录. 1.gradle 三个gradle script // Top-level build ...

  8. QT学习小demo之LightMD(MarkDown编辑器)

    很早之前就有了写一个类似Windows记事本的想法,加上最近也刚好在学编译原理,所以就想把两者结合起来,于是就打算结合MarkDown,开发一款MarkDown编辑器. 不过由于我之前一直使用的是Ja ...

  9. android学习小例子——验证码倒计时按钮

    1.activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

随机推荐

  1. 关于system(”pause“);的作用和意义

    注意:不要再return 的语句之后加,那样就执行不到了. system() 是调用系统命令:pause 暂停命令: 如果加有  system(”pause“): 这样在运行到此处时,会显示“Pres ...

  2. vmware中ubuntu更新内核后无法进入桌面,鼠标“漂移”滑动

    问题背景: 我机子上是在vmware下安装了ubuntu12.04,今天正在ubuntu下工作,结果提示内核有更新,手贱的就点了个OK,开始更新,更新完重启.结果,问题来了,刚开始系统启动,进入系统登 ...

  3. 均值,方差: 概率质量函数PMF

    __author__ = 'dell' import Pmf import matplotlib.pyplot as pyplot pmf = Pmf.MakePmfFromList([1, 2, 2 ...

  4. java Active Object模式(下)

    Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...

  5. LXC是如何与CGROUP,namespace扯上关系的?再加上DOCKER.IO。完美!!!

    最后还余下网络去攻克了. 不同的模板,只是在同一个LINUX内核上去实现不同的发行版的特性. 终归,都是用同样的内核来实现调度.故而是一个轻量极的方案. 而不像KVM一样,GUEST OS里的CPU也 ...

  6. db2数据库中查找数据库表

    模糊查找db2数据库中的数据库表: select tabname,remarks from syscat.tables where TABNAME like  'DM%' select 'DROP T ...

  7. Struts 2 标签

    注:要使用Strust 2标签需<%@ taglib prefix="s" uri="/struts-tags" %> 表单标签: .form标签 ...

  8. mysql数据库操作语句大全

    一 . 常用mysql命令行命令 1 .启动MYSQL服务  net start mysql 停止MYSQL服务  net stop mysql 2 . netstat –na | findstr 3 ...

  9. 【HDOJ】2645 find the nearest station

    裸BFS. /* 2645 */ #include <iostream> #include <queue> #include <cstdio> #include & ...

  10. France \'98(概率)

    题目描述 Today the first round of the Soccer World Championship in France is coming to an end. 16 countr ...