文章出处:http://blog.csdn.net/harvic880925/article/details/24416131,谢谢
一、新建类,派生自EditText
新建类UnderLineEditText,截图如下:
代码如下:
- package com.example.underlineedittext;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.EditText;
- public class UnderLineEditText extends EditText {
- private Paint linePaint;
- private int paperColor;
- public UnderLineEditText(Context context,AttributeSet paramAttributeSet) {
- super(context,paramAttributeSet);
- // TODO Auto-generated constructor stub
- this.linePaint = new Paint();
- linePaint.setColor(Color.GRAY);//设置下划线颜色
- }
- 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();
- for (int i2 = 0;; i2++) {
- if (i2 >= i) {
- super.onDraw(paramCanvas);
- paramCanvas.restore();
- return;
- }
- n += k;
- paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
- paramCanvas.save();
- }
- }
- }
解析:
就是得到总共有多少行(m),然后根据每行的行高划线。
二、XML使用
XML使用
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <com.example.underlineedittext.UnderLineEditText
- android:id="@+id/eg1"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:textSize="20dip"
- android:background="@null"
- android:gravity="top"
- android:lineSpacingMultiplier="2"
- android:inputType="textMultiLine" />
- </LinearLayout>
其中<com.example.underlineedittext.UnderLineEditText中UnderLineEditText类的所在位置!这就是自定义扩展控件的使用方法,如果要在代码中对其操作,与其它控件一样,利用findViewById()……
三、输入法弹出窗口自适应
在AndroidManifest.xml对应的Activity中添加下面代码以使在弹出输入法时,我们的EditText窗体能自适应高度。
- android:windowSoftInputMode="stateHidden|adjustResize"
整体的AndroidManifest.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.underlineedittext"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="8" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.underlineedittext.MainActivity"
- android:windowSoftInputMode="stateHidden|adjustResize"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
效果图如下:
四、问题及改进
问题:
从上面的效果图可以看出:文字没有紧贴着底线,这是为什么呢?这是因为我们在XML中设置了android:lineSpacingMultiplier="2"参数(行间距变为原来的2倍),如果没有此参数,就一切正常了,但就此问题,我们加以解决。
解决办法:
文字与下划线多出来的距离应该是行高减去文字的高度,所以,我们在计算出每次的划线位置后,先减去这个高度,就得到当前的划线位置。
代码如下:
- package com.example.underlineedittext;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.EditText;
- public class UnderLineEditText extends EditText {
- private Paint linePaint;
- private int paperColor;
- public UnderLineEditText(Context context,AttributeSet paramAttributeSet) {
- super(context,paramAttributeSet);
- // TODO Auto-generated constructor stub
- this.linePaint = new Paint();
- linePaint.setColor(Color.GRAY);//设置下划线颜色
- }
- 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();
- int distance_with_btm=(int) (getLineHeight()-getTextSize())-3;
- //这个关于距离底部的变量当不使用lineSpacingMultiplier和lineSpacingExtra参数时是不起作用的
- for (int i2 = 0;; i2++) {
- if (i2 >= i) {
- super.onDraw(paramCanvas);
- paramCanvas.restore();
- return;
- }
- n += k;
- n-=distance_with_btm;//将线划在字体靠下面
- paramCanvas.drawLine(0.0F, n, getRight(), n, this.linePaint);
- paramCanvas.save();
- n+=distance_with_btm;//还原n
- }
- }
- }
这里较上面的代码多出了distance_with_btm变量。
效果:
这里是解决了文字紧贴底部显示,但问题又出现了,第一行的行高明显与其它行的行高不一样!这里之所以这么明显是因为行高倍数设置为了2,只是为了突显这个效果而已,在实际代码中可以使用1.3左右的倍数,基本上是看不出差别的。
/***************************************************************************************************************************************************************************************/
本人写的时候用的1.3,但是依旧还是很明显第一行。我的解决方案是将整个空间设置marginTop来拼凑不足的像素。