閱讀94 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android關於實現EditText中加多行下劃線的的一種方法

1. 重寫EditText

public class LinedEditText extends EditText {
     private Paint linePaint;
     private float margin;
     private int paperColor;
 
     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:andro
     xmlns:tools="https://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
 
     <com.example.storetest.LinedEditText
         android:
         android:layout_width="fill_parent"
         android:layout_height="200dp"
         android:gravity="top"
         android:inputType="textMultiLine" />
 
 </LinearLayout>

注意:使用EditText控件時,不再使用EditText前綴,而是重寫之後完整路徑的包名+類名(如com.example.storetest.LinedEditText)。


3.在使用時與正常調用EditText時一致。


最後更新:2017-04-02 15:15:07

  上一篇:go object-c 多線程 加鎖
  下一篇:go 長噓一口氣。。。