自定义EditText 去除边框,添加下划线

原文地址:http://androidstudy.iteye.com/blog/770512

首先:重写EditText

Java代码  收藏代码
  1. //请在这里添加您的包名  
  2. import android.content.Context;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.util.AttributeSet;  
  7. import android.widget.EditText;  
  8.   
  9. public class LineEditText extends EditText {  
  10.   
  11.     private Paint mPaint;  
  12.     /** 
  13.      * @param context 
  14.      * @param attrs 
  15.      */  
  16.     public LineEditText(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.         // TODO Auto-generated constructor stub  
  19.         mPaint = new Paint();  
  20.           
  21.         mPaint.setStyle(Paint.Style.STROKE);  
  22.         mPaint.setColor(Color.RED);  
  23.     }  
  24.       
  25.     @Override  
  26.     public void onDraw(Canvas canvas)  
  27.     {  
  28.         super.onDraw(canvas);  
  29.           
  30. //      画底线  
  31.         

            canvas.drawLine(0,this.getHeight()-1,  this.getWidth()-1, this.getHeight()-1, mPaint);  

            canvas.drawLine(this.getWidth() - 1, this.getHeight(), this.getWidth() - 1, this.getHeight()/4, mPaint);  

            canvas.drawLine(0, this.getHeight(), 0, this.getHeight()/4, mPaint); 

  32.     }  
  33. }  

 

其次:设置LineEditText(XML文件)

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6. <!--注意名称 -->  
  7. <com.marine.study.LineEditText   
  8.       
  9.     android:id="@+id/myEdit"  
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     style="?android:attr/textViewStyle"   
  13.     android:background="@null"  
  14.     android:textColor="@null"   
  15. />  
  16. </LinearLayout>  

 其中background,可以设置成其他颜色等

textColor不一定要是null,可以设置字体颜色

 

 

效果图:

  1.  

 

posted @ 2014-03-12 16:11  下一次ai微笑  阅读(605)  评论(0)    收藏  举报