自定义EditText
来源:互联网 发布:邮币卡电子盘交易软件 编辑:程序博客网 时间:2024/05/16 08:38
这几天想要做一种类似于淘宝App的登录界面,看着淘宝上的EditText真是好看,它换了底线,换了光标。应该是继承EditText后重写的onDrow方法。那么它具体是怎么实现的呢?经过一段时间的摸索后终于实现了类似的效果。
首先我们看看如果更改光标样式
这个主要是这么一个参数:
android:textCursorDrawable
给它配一个shape文件就可以了。关于shape我这里有一篇博客关于shap 可供学习。
这是我写的一个shape文件示例:
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <size android:width="1dp"/> <solid android:color="@color/colorPrimary"/></shape>
然后我们来看看如何更改EditText的底部线
这个就需要继承EditText了,我们先写一个类继承它:
public class MyEditText extends android.support.v7.widget.AppCompatEditText {public MyEditText(Context context) { super(context);}public MyEditText(Context context, AttributeSet attrs) { super(context, attrs);}public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr);}}
这里我们需要写三个构造方法。
然后我们需要定义两个画笔:Paint
private Paint mPaint;private Paint wPaint;
mPaint用来画一个大矩形
wPaint用来画一个小矩形
这样他两个重叠在一起通过控制就会在底部出来一条线。这样我们就完成了目标。
我们先将画笔初始化:
private void init(){ Resources resources=getResources(); paperColor=resources.getColor(R.color.colorPrimary); mPaint=new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(paperColor); mPaint.setStyle(Paint.Style.FILL); wPaint=new Paint(Paint.ANTI_ALIAS_FLAG); wPaint.setColor(resources.getColor(R.color.color_white)); wPaint.setStyle(Paint.Style.FILL);}
这样,画笔就被初始化了。
然后,我们重写onDrow方法:
@Overrideprotected void onDraw(Canvas canvas) { canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight()-2,wPaint); canvas.save(); super.onDraw(canvas); canvas.restore();}
整体代码就是:
public class MyEditText extends android.support.v7.widget.AppCompatEditText {private Paint mPaint;private Paint wPaint;private int paperColor;public MyEditText(Context context) { super(context); init();}public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); init();}public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init();}private void init(){ Resources resources=getResources(); paperColor=resources.getColor(R.color.colorPrimary); mPaint=new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(paperColor); mPaint.setStyle(Paint.Style.FILL); wPaint=new Paint(Paint.ANTI_ALIAS_FLAG); wPaint.setColor(resources.getColor(R.color.color_white)); wPaint.setStyle(Paint.Style.FILL);}@Overrideprotected void onDraw(Canvas canvas) { canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight()-2,wPaint); canvas.save(); super.onDraw(canvas); canvas.restore();}
}
阅读全文
0 0
- EditText自定义
- 自定义EditText
- 自定义EditText
- 自定义EditText
- 自定义EditText
- 自定义EditText
- 自定义EditText
- android editText 自定义边框
- 自定义EditText控件
- android 自定义edittext边框
- android自定义EditText
- Edittext自定义边框颜色
- 如何自定义EditText样式
- 自定义edittext边框
- 自定义EditText样式
- android EditText自定义样式
- Android EditText样式自定义
- android EditText自定义样式
- 第六天
- 伪随机数
- shell脚本编程实现多彩进度条
- 015-认知能量|减少心力的流失
- 【CJOJ1644】【洛谷2758】编辑距离
- 自定义EditText
- Scope注解
- 《软件测试》 --- 读书笔记
- 清除wnTKYg 这个挖矿工木马的过程讲述
- 20170703
- RxJava 1.x 实现简单的logService
- Python笔记
- Stanford Log-linear Part-Of-Speech Tagger学习
- 成员变量和局部变量