Android 如何自定义EditText 下划线?(android安卓软件下载)

网友投稿 974 2022-09-10

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

Android 如何自定义EditText 下划线?(android安卓软件下载)

记录制作过程:

第一版本

public class LineEditText extends EditText {

private Paint mPaint;private int color;public static final int STATUS_FOCUSED = 1;public static final int STATUS_UNFOCUSED = 2;public static final int STATUS_ERROR = 3;private int status = 2;private Drawable del_btn;private Drawable del_btn_down;private int focusedDrawableId = R.drawable.user_select;// 默认的private int unfocusedDrawableId = R.drawable.user;private int errorDrawableId = R.drawable.user_error;Drawable left = null;private Context mContext;

public LineEditText(Context context) {

super(context); mContext = context; init();}

public LineEditText(Context context, AttributeSet attrs) {

super(context, attrs); mContext = context; init();}

public LineEditText(Context context, AttributeSet attrs, int defStryle) {

super(context, attrs, defStryle); mContext = context; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lineEdittext, defStryle, 0); focusedDrawableId = a.getResourceId( R.styleable.lineEdittext_drawableFocus, R.drawable.user_select); unfocusedDrawableId = a.getResourceId( R.styleable.lineEdittext_drawableUnFocus, R.drawable.user); errorDrawableId = a.getResourceId( R.styleable.lineEdittext_drawableError, R.drawable.user_error); a.recycle(); init();}

/** * 2014/7/31 * * @author Aimee.ZHANG */

// left.setBounds(0, 0, 30, 40);// this.setCompoundDrawables(left, null, null, null);

setCompoundDrawablesWithIntrinsicBounds(left,null,del_btn,null); } postInvalidate();}public void setLeftDrawable(int focusedDrawableId, int unfocusedDrawableId, int errorDrawableId) { this.focusedDrawableId = focusedDrawableId; this.unfocusedDrawableId = unfocusedDrawableId; this.errorDrawableId = errorDrawableId; setStatus(status);}@Overrideprotected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused) { setStatus(STATUS_FOCUSED); } else { setStatus(STATUS_UNFOCUSED); }}@Overrideprotected void finalize() throws Throwable { super.finalize();};public void setColor(int color) { this.color = color; this.setTextColor(color); invalidate();}

}

代码解释:

变量名 STATUS_FOCUSED,STATUS_UNFOCUSED,STATUS_ERROR 标示了三种状态,选中状况为蓝色,未选中状态为灰色,错误状态为红色。focusedDrawableId unfocusedDrawableId errorDrawableId存放三种状态的图片,放置于最左侧。

因此有了第二个版本

public class LineEditText extends EditText implements TextWatcher, OnFocusChangeListener{

private Paint mPaint;private int color;public static final int STATUS_FOCUSED = 1;public static final int STATUS_UNFOCUSED = 2;public static final int STATUS_ERROR = 3;private int status = 2;private Drawable del_btn;private Drawable del_btn_down;private int focusedDrawableId = R.drawable.user_select;// 默认的private int unfocusedDrawableId = R.drawable.user;private int errorDrawableId = R.drawable.user_error;Drawable left = null;private Context mContext;/** * 是否获取焦点,默认没有焦点 */ private boolean hasFocus = false; /** * 手指抬起时的X坐标 */ private int xUp = 0; public LineEditText(Context context) { super(context); mContext = context; init();}public LineEditText(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init();}public LineEditText(Context context, AttributeSet attrs, int defStryle) { super(context, attrs, defStryle); mContext = context; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lineEdittext, defStryle, 0); focusedDrawableId = a.getResourceId( R.styleable.lineEdittext_drawableFocus, R.drawable.user_select); unfocusedDrawableId = a.getResourceId( R.styleable.lineEdittext_drawableUnFocus, R.drawable.user); errorDrawableId = a.getResourceId( R.styleable.lineEdittext_drawableError, R.drawable.user_error); a.recycle(); init();}/** * 2014/7/31 * * @author Aimee.ZHANG */private void init() { mPaint = new Paint(); // mPaint.setStyle(Paint.Style.FILL); mPaint.setStrokeWidth(3.0f); color = Color.parseColor("#bfbfbf"); setStatus(status); del_btn = mContext.getResources().getDrawable(R.drawable.del_but_bg); del_btn_down = mContext.getResources().getDrawable(R.drawable.del_but_bg_down); addListeners(); setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);}@Overrideprotected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(color); canvas.drawLine(0, this.getHeight() - 1, this.getWidth(), this.getHeight() - 1, mPaint);}// 删除图片

// private void setDrawable() { // if (length() < 1) { // setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); // } else { // setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn,null); // } // }

// left.setBounds(0, 0, 30, 40); // this.setCompoundDrawables(left, null, null, null); setCompoundDrawablesWithIntrinsicBounds(left,null,null,null); } postInvalidate(); }

public void setLeftDrawable(int focusedDrawableId, int unfocusedDrawableId, int errorDrawableId) { this.focusedDrawableId = focusedDrawableId; this.unfocusedDrawableId = unfocusedDrawableId; this.errorDrawableId = errorDrawableId; setStatus(status);} private void addListeners() { try { setOnFocusChangeListener(this); addTextChangedListener(this); } catch (Exception e) { e.printStackTrace(); } } @Overrideprotected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); this.hasFocus=focused; if (focused) { setStatus(STATUS_FOCUSED); } else { setStatus(STATUS_UNFOCUSED); setCompoundDrawablesWithIntrinsicBounds(left,null,null,null); }}@Overrideprotected void finalize() throws Throwable { super.finalize();};public void setColor(int color) { this.color = color; this.setTextColor(color); invalidate();}@Overridepublic void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub postInvalidate();}@Overridepublic void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub if (TextUtils.isEmpty(arg0)) { // 如果为空,则不显示删除图标 setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } else { // 如果非空,则要显示删除图标 setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn, null); } }@Override public void onTextChanged(CharSequence s, int start, int before, int after) { if (hasFocus) { if (TextUtils.isEmpty(s)) { // 如果为空,则不显示删除图标 setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } else { // 如果非空,则要显示删除图标 setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn, null); } }

}

@Overridepublic void onFocusChange(View arg0, boolean arg1) { // TODO Auto-generated method stub try { this.hasFocus = arg1; } catch (Exception e) { e.printStackTrace(); } }

}

比较关键的方法是:onTouchEvent

存在的问题:这个版本依旧存在问题,就是输入长度超过输入框,所画的线不会延伸,如图

解决方法:

@Override

protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(color); int x=this.getScrollX(); int w=this.getMeasuredWidth(); canvas.drawLine(0, this.getHeight() - 1, w+x, this.getHeight() - 1, mPaint);}

w:获取控件长度X:延伸后的长度

在分享完这个界面的代码设计后,笔者跟大家唠一些新玩意。话说身处在帝都,如果不利用好帝都的丰厚资源,又如何对得起每天吸入的几十斤雾霾?

话唠的分享

在帝都生活,我每天早晨起来都会告诉自己,又是新的一天,要认真过。

什么是 APM?

In the fields of information technology and systems management, Application Performance Management (APM) is the monitoring and management of performance and availability of software applications. APM strives to detect and diagnose complex application performance problems to maintain an expected level of service. APM is "the translation of IT metrics into business meaning .

卡顿趋势图:随时间的推移,反馈卡顿发生次数的趋势情况设备分布图:卡顿现象集中分布的设备类型卡顿页面:发生卡顿的页面有哪些,其中平均流畅度是多少,卡顿了多少次等信息。

查看单个页面的卡顿情况,并从页面线程加载的情况中分析造成卡顿原因

如果你也想检验一下自己所写的 APP 的用户体验情况,不妨试试这个新玩意~~

上一篇:如何对 Android 库进行依赖管理?
下一篇:减少 WAF 漏报的 8 种方法 !(减少百分比的计算公式)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~