Android--去除EditText边框,添加下划线

转载:

      http://blog.sina.com.cn/s/blog_59b30fd40100q56c.html

//请在这里添加您的包名   
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 LineEditText extends EditText {   
   
    private Paint mPaint;   
       
    public LineEditText(Context context, AttributeSet attrs) {   
        super(context, attrs);   
        // TODO Auto-generated constructor stub   
        mPaint = new Paint();   
           
        mPaint.setStyle(Paint.Style.STROKE);   
        mPaint.setColor(Color.BLUE);   
    }   
       
    @Override   
    public void onDraw(Canvas canvas)   
    {   
        super.onDraw(canvas);   
           
//      画底线   
        canvas.drawLine(0,this.getHeight()-1,  this.getWidth()-1, this.getHeight()-1, mPaint);   
    }   
}   
//请在这里添加您的包名 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 LineEditText extends EditText { private Paint mPaint; public LineEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub mPaint = new Paint(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.BLUE); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); // 画底线 canvas.drawLine(0,this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, mPaint); } }
 

其次:设置LineEditText

Java代码  
<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    >   
<!--注意名称 -->   
<com.marine.study.LineEditText    
       
    android:id="@+id/myEdit"   
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    style="?android:attr/textViewStyle"    
    android:background="@null"   
    android:textColor="@null"    
/>   
</LinearLayout>   
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!--注意名称 --> <com.marine.study.LineEditText android:id="@+id/myEdit" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/textViewStyle" android:background="@null" android:textColor="@null" /> </LinearLayout>
 其中background,可以设置成其他颜色等

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

 

最后:应用LineEditText

 

Java代码  
import android.app.Activity;   
import android.content.Context;   
import android.graphics.Canvas;   
import android.graphics.Color;   
import android.graphics.Paint;   
import android.graphics.Rect;   
import android.os.Bundle;   
import android.text.InputFilter;   
import android.util.AttributeSet;   
import android.widget.EditText;   
   
public class LineEditTextTest extends Activity {   
       
    private EditText mEdit;   
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
           
        mEdit=(LineEditText)findViewById(R.id.myEdit);   
        mEdit.setHint("从这里开始输入...");   
        mEdit.setSingleLine();   
        mEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});   
    }   
       
}

相关推荐