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

1. 重写EditText

  1. public class LinedEditText extends EditText {
  2.      private Paint linePaint;
  3.      private float margin;
  4.      private int paperColor;

  5.      public LinedEditText(Context paramContext, AttributeSet paramAttributeSet) {
  6.          super(paramContext, paramAttributeSet);
  7.          this.linePaint = new Paint();
  8.      }

  9.      protected void onDraw(Canvas paramCanvas) {
  10.          paramCanvas.drawColor(this.paperColor);
  11.          int i = getLineCount();
  12.          int j = getHeight();
  13.          int k = getLineHeight();
  14.          int m = 1 + j / k;
  15.          if (i < m)
  16.              i = m;
  17.          int n = getCompoundPaddingTop();
  18.          paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
  19.          for (int i2 = 0;; i2++) {
  20.              if (i2 >= i) {
  21.                  setPadding(10 + (int) this.margin, 0, 0, 0);
  22.                  super.onDraw(paramCanvas);
  23.                  paramCanvas.restore();
  24.                  return;
  25.              }
  26.              n += k;
  27.              paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
  28.              paramCanvas.save();
  29.          }
  30.      }

  31. }
复制代码
这段代码,并不复杂没有加注释,学过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.      <com.example.storetest.LinedEditText
  7.          android:id="@+id/et1"
  8.          android:layout_width="fill_parent"
  9.          android:layout_height="200dp"
  10.          android:gravity="top"
  11.          android:inputType="textMultiLine" />

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

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

转载于:https://my.oschina.net/u/698243/blog/87398

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值