android关于实现EditText中加多行下划线的的一种方法

1. 重写EditText

 1 public class LinedEditText extends EditText {
 2     private Paint linePaint;
 3     private float margin;
 4     private int paperColor;
 5 
 6     public LinedEditText(Context paramContext, AttributeSet paramAttributeSet) {
 7         super(paramContext, paramAttributeSet);
 8         this.linePaint = new Paint();
 9     }
10 
11     protected void onDraw(Canvas paramCanvas) {
12         paramCanvas.drawColor(this.paperColor);
13         int i = getLineCount();
14         int j = getHeight();
15         int k = getLineHeight();
16         int m = 1 + j / k;
17         if (i < m)
18             i = m;
19         int n = getCompoundPaddingTop();
20         paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
21         for (int i2 = 0;; i2++) {
22             if (i2 >= i) {
23                 setPadding(10 + (int) this.margin, 0, 0, 0);
24                 super.onDraw(paramCanvas);
25                 paramCanvas.restore();
26                 return;
27             }
28             n += k;
29             paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
30             paramCanvas.save();
31         }
32     }
33 
34 }

这段代码,并不复杂没有加注释,学过Java的同学应该不会吃力。

2.在布局文件中使用

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <com.example.storetest.LinedEditText
 8         android:id="@+id/et1"
 9         android:layout_width="fill_parent"
10         android:layout_height="200dp"
11         android:gravity="top"
12         android:inputType="textMultiLine" />
13 
14 </LinearLayout>

注意:使用EditText控件时,不再使用EditText前缀,而是重写之后完整路径的包名+类名(如com.example.storetest.LinedEditText)。


3.在使用时与正常调用EditText时一致。

——整理自网络,多多分享

posted on 2012-10-08 15:39  公子红  阅读(1234)  评论(1编辑  收藏  举报