重写EditText,使其具有下划线

来源:互联网 发布:sdrsharp软件下载 编辑:程序博客网 时间:2024/06/05 05:33

一、新建类,派生自EditText

新建类UnderLineEditText,截图如下:

代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.underlineedittext;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.widget.EditText;  
  9.   
  10. public class UnderLineEditText extends EditText {  
  11.       
  12.     private Paint linePaint;  
  13.     private int paperColor;  
  14.   
  15.     public UnderLineEditText(Context context,AttributeSet paramAttributeSet) {  
  16.         super(context,paramAttributeSet);  
  17.         // TODO Auto-generated constructor stub  
  18.         this.linePaint = new Paint();  
  19.         linePaint.setColor(Color.GRAY);//设置下划线颜色  
  20.     }  
  21.       
  22.     protected void onDraw(Canvas paramCanvas) {  
  23.         paramCanvas.drawColor(this.paperColor); //设置背景色  
  24.         int i = getLineCount();  
  25.         int j = getHeight();  
  26.         int k = getLineHeight();  
  27.         int m = 1 + j / k;  
  28.         if (i < m) i = m;  
  29.         int n = getCompoundPaddingTop();  
  30.   
  31.         for (int i2 = 0;; i2++) {  
  32.             if (i2 >= i) {  
  33.                 super.onDraw(paramCanvas);  
  34.                 paramCanvas.restore();  
  35.                 return;  
  36.             }  
  37.               
  38.             n += k;  
  39.             paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);  
  40.             paramCanvas.save();  
  41.         }  
  42.     }  
  43.   
  44. }  

解析:

就是得到总共有多少行(m),然后根据每行的行高划线。

二、XML使用

XML使用

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <com.example.underlineedittext.UnderLineEditText  
  9.          android:id="@+id/eg1"  
  10.          android:layout_width="fill_parent"  
  11.          android:layout_height="match_parent"  
  12.          android:textSize="20dip"  
  13.          android:background="@null"  
  14.          android:gravity="top"  
  15.          android:lineSpacingMultiplier="2"  
  16.          android:inputType="textMultiLine" />  
  17.   
  18. </LinearLayout>  

其中<com.example.underlineedittext.UnderLineEditText中UnderLineEditText类的所在位置!这就是自定义扩展控件的使用方法,如果要在代码中对其操作,与其它控件一样,利用findViewById()……

三、输入法弹出窗口自适应

在AndroidManifest.xml对应的Activity中添加下面代码以使在弹出输入法时,我们的EditText窗体能自适应高度。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. android:windowSoftInputMode="stateHidden|adjustResize"  

整体的AndroidManifest.xml代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.underlineedittext"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="8" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.underlineedittext.MainActivity"  
  18.             android:windowSoftInputMode="stateHidden|adjustResize"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  

效果图如下:

 

 四、问题及改进

问题:

 从上面的效果图可以看出:文字没有紧贴着底线,这是为什么呢?这是因为我们在XML中设置了android:lineSpacingMultiplier="2"参数(行间距变为原来的2倍),如果没有此参数,就一切正常了,但就此问题,我们加以解决。

 解决办法:

 文字与下划线多出来的距离应该是行高减去文字的高度,所以,我们在计算出每次的划线位置后,先减去这个高度,就得到当前的划线位置。

代码如下:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.underlineedittext;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.widget.EditText;  
  9.   
  10. public class UnderLineEditText extends EditText {  
  11.       
  12.     private Paint linePaint;  
  13.     private int paperColor;  
  14.   
  15.     public UnderLineEditText(Context context,AttributeSet paramAttributeSet) {  
  16.         super(context,paramAttributeSet);  
  17.         // TODO Auto-generated constructor stub  
  18.         this.linePaint = new Paint();  
  19.         linePaint.setColor(Color.GRAY);//设置下划线颜色  
  20.     }  
  21.       
  22.     protected void onDraw(Canvas paramCanvas) {  
  23.         paramCanvas.drawColor(this.paperColor); //设置背景色  
  24.         int i = getLineCount();  
  25.         int j = getHeight();  
  26.         int k = getLineHeight();  
  27.         int m = 1 + j / k;  
  28.         if (i < m) i = m;  
  29.         int n = getCompoundPaddingTop();  
  30.           
  31.         int distance_with_btm=(int) (getLineHeight()-getTextSize())-3;  
  32.         //这个关于距离底部的变量当不使用lineSpacingMultiplier和lineSpacingExtra参数时是不起作用的  
  33.   
  34.         for (int i2 = 0;; i2++) {  
  35.             if (i2 >= i) {  
  36.                 super.onDraw(paramCanvas);  
  37.                 paramCanvas.restore();  
  38.                 return;  
  39.             }  
  40.               
  41.             n += k;  
  42.             n-=distance_with_btm;//将线划在字体靠下面  
  43.             paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);  
  44.             paramCanvas.save();  
  45.             n+=distance_with_btm;//还原n  
  46.         }  
  47.     }  
  48.   
  49. }  

这里较上面的代码多出了distance_with_btm变量。

效果:

这里是解决了文字紧贴底部显示,但问题又出现了,第一行的行高明显与其它行的行高不一样!这里之所以这么明显是因为行高倍数设置为了2,只是为了突显这个效果而已,在实际代码中可以使用1.3左右的倍数,基本上是看不出差别的。

 

 源码地址:http://download.csdn.net/detail/harvic880925/7245941,不要分,仅供分享

 

 请大家尊重作者原创版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/24416131,谢谢

0 0