当前位置:操作系统 > 安卓/Android >>

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

1. 重写EditText


public class LinedEditText extends EditText {

     private Paint linePaint;

     private float margin;

     private int 易做图Color; www.zzzyk.com

 

     public LinedEditText(Context paramContext, AttributeSet paramAttributeSet) {

         super(paramContext, paramAttributeSet);

         this.linePaint = new Paint();

     }

 

     protected void onDraw(Canvas paramCanvas) {

         paramCanvas.drawColor(this.易做图Color);

         int i = getLineCount();

         int j = getHeight();

         int k = getLineHeight();

         int m = 1 + j / k;

         if (i < m)

             i = m;

         int n = getCompoundPaddingTop();

         paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);

         for (int i2 = 0;; i2++) {

             if (i2 >= i) {

                 setPadding(10 + (int) this.margin, 0, 0, 0);

                 super.onDraw(paramCanvas);

                 paramCanvas.restore();

                 return;

             }

             n += k;

             paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);

             paramCanvas.save();

         }

     }

 

}


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

2.在布局文件中使用


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

     xmlns:tools="http://schemas.android.com/tools"

     android:layout_width="match_parent"

     android:layout_height="match_parent"

     android:orientation="vertical" >

 

     <com.example.storetest.LinedEditText

         android:id="@+id/et1"

         android:layout_width="fill_parent"

         android:layout_height="200dp"

         android:gravity="top"

         android:inputType="textMultiLine" />

 

</LinearLayout>

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,