android實現EditText中加多行底線的一種方法

來源:互聯網
上載者:User

1. 重寫EditText


public class LinedEditText extends EditText {

     private Paint linePaint;

     private float margin;

     private int paperColor; www.2cto.com

 

     public LinedEditText(Context paramContext, AttributeSet paramAttributeSet) {

         super(paramContext, paramAttributeSet);

         this.linePaint = new Paint();

     }

 

     protected void onDraw(Canvas paramCanvas) {

         paramCanvas.drawColor(this.paperColor);

         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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.