android 可以任意继承控件而写出自定义的漂亮的控件来。要写自定义的可以需要继续android.widget.EditText,并且重写其它onDraw就可以得到自己的编辑框。
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.WindowManager; import android.widget.EditText; public class TelEdit extends EditText{ Context mContext; public TelEdit(Context context) { super(context); mContext = context; } public TelEdit(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public TelEdit(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; } protected void onDraw(Canvas canvas) { WindowManager wm = (WindowManager) mContext.getSystemService("window"); int windowWidth = wm.getDefaultDisplay().getWidth(); int windowHeight = wm.getDefaultDisplay().getHeight(); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLACK); int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom(); int scrollY = getScrollY(); int scrollX = getScrollX()+windowWidth; int innerHeight = scrollY + getHeight() - paddingTop - paddingBottom; int lineHeight = getLineHeight(); int baseLine = scrollY + (lineHeight - (scrollY % lineHeight)); int x = 8; while (baseLine < innerHeight) { //canvas.drawBitmap(line, x, baseLine + paddingTop, paint); canvas.drawLine(x, baseLine + paddingTop,scrollX, baseLine + paddingTop, paint); baseLine += lineHeight; } super.onDraw(canvas); } }
使用如下代码插入:
<com.xxx.tel.common.TelEdit android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="fill_parent" android:background="@drawable/xxxx" android:gravity="top" android:typeface="sans" android:capitalize="sentences" android:lineSpacingExtra="5dip" android:textSize="22dip" android:textColor="@color/wite" android:layout_height="300dip"> </com.xxx.tel.common.TelEdit>