本文实例为大家分享了Android自定义控件EditText的具体代码,供大家参考,具体内容如下

自定义控件分三种:

1. 自绘控件

2. 组合控件

3. 继承控件

代码已上传到 github

以后的自定义控件就都放这个仓库

需求

这里由于项目的需要实现一个自定义EditText,主要实现的为两点,一个是工具图标toolIcon,例如点击清除EditText内容。一个为EditText左边的提示图标hintIcon, 例如输入账号密码时前面的图标。

为了让这个控件的拓展性更高,设置了两个点击事件接口。对于toolIcon来说,默认点击事件为清除EditText内容,如果需要更改,在代码中设设置相关的点击事件即可。

步骤

继承EditText

编写attrs.xml, 创建declare-styleable

编写MyEditText

布局中使用

实现

获取布局文件中设置的属性

这里返回的是一个TypedArray数组,获取之后就可以获得布局文件中设置的属性了private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.getTheme().obtainStyledAttributes( attrs, R.styleable.MyEditText, 0, 0); hintIcon = typedArray.getDrawable(R.styleable.MyEditText_hintIcon); toolIcon = typedArray.getDrawable(R.styleable.MyEditText_toolIcon); fixed = typedArray.getBoolean(R.styleable.MyEditText_fixed, true); if (toolIcon != null && fixed) { setHeight(toolIcon.getIntrinsicHeight()); } setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); setCompoundDrawablePadding(10); typedArray.recycle(); onClickListenerWithEditTextToolIcon = new OnClickListenerWithEditTextToolIcon() { @Override public void onClick() { setText(""); } }; }

设置资源图片

EditText是继承自TextView,在TextView中存在两个方法setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)setCompoundDrawables(left, top, right, bottom)

是设置资源图片的位置,第一个方法和第二个方法的区别在于第一个方法中资源图片的大小是由系统来获取图片固有的大小,第二个方法则是需要自己通过LayoutParams设置大小。

设置点击事件

我们通过setCompoundDrawables()等方法设置的图片,而由于在父类中并没有提供相关的图片点击处理接口,因此可以重写onTouchEvent()来实现相关的点击事件,只需要根据我们手指落点或抬起点的位置就可以判断手指是否点击了相关图片。在这里,我选择了手指抬起时处理/** * Override the touchEvent to judge whether click toolIcon or hintIcon * * @param event motionEvent * @return super */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (hintIcon != null) { if (event.getX() < hintIcon.getIntrinsicWidth() && event.getX() > 0) { if (getCompoundDrawables()[0] != null && onClickListenerWithEditTextHintIcon != null) { onClickListenerWithEditTextHintIcon.onClick(); } } } if (toolIcon != null) { if (event.getX() > (getWidth() - toolIcon.getIntrinsicWidth()) && event.getX() < getWidth()) { if (getCompoundDrawables()[2] != null ) { onClickListenerWithEditTextToolIcon.onClick(); } } } } return super.onTouchEvent(event); }/** * interface when click hintIcon */ public interface OnClickListenerWithEditTextHintIcon { void onClick(); } /** * interface when click toolIcon */ public interface OnClickListenerWithEditTextToolIcon { void onClick(); }

完整代码package com.customwidget.lzqwidget.cuswidget;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.EditText;import com.customwidget.lzqwidget.R;/** * Custom widget of EditText with two icon. * Created by lizhongquan on 16-1-6. */public class MyEditText extends EditText { private Drawable hintIcon = null; private Drawable toolIcon = null; /** * HintIcon clickListener */ private OnClickListenerWithEditTextHintIcon onClickListenerWithEditTextHintIcon = null; /** * Default clear the EditText */ private OnClickListenerWithEditTextToolIcon onClickListenerWithEditTextToolIcon = null; /** * Default fixed the Height */ private boolean fixed = true; public MyEditText(Context context) { super(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.getTheme().obtainStyledAttributes( attrs, R.styleable.MyEditText, 0, 0); hintIcon = typedArray.getDrawable(R.styleable.MyEditText_hintIcon); toolIcon = typedArray.getDrawable(R.styleable.MyEditText_toolIcon); fixed = typedArray.getBoolean(R.styleable.MyEditText_fixed, true); if (toolIcon != null && fixed) { setHeight(toolIcon.getIntrinsicHeight()); } setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); setCompoundDrawablePadding(10); typedArray.recycle(); onClickListenerWithEditTextToolIcon = new OnClickListenerWithEditTextToolIcon() { @Override public void onClick() { setText(""); } }; } /** * Override the touchEvent to judge whether click toolIcon or hintIcon * * @param event motionEvent * @return super */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (hintIcon != null) { if (event.getX() < hintIcon.getIntrinsicWidth() && event.getX() > 0) { if (getCompoundDrawables()[0] != null && onClickListenerWithEditTextHintIcon != null) { onClickListenerWithEditTextHintIcon.onClick(); } } } if (toolIcon != null) { if (event.getX() > (getWidth() - toolIcon.getIntrinsicWidth()) && event.getX() < getWidth()) { if (getCompoundDrawables()[2] != null ) { onClickListenerWithEditTextToolIcon.onClick(); } } } } return super.onTouchEvent(event); } /** * the clickListener of click hintIcon * * @param clickListenerOfHintIcon OnClickListenerWithEditTextHintIcon */ public void setOnClickListenerWithEditTextHintIcon( OnClickListenerWithEditTextHintIcon clickListenerOfHintIcon) { this.onClickListenerWithEditTextHintIcon = clickListenerOfHintIcon; } /** * the clickListener of click toolIcon * * @param clickListenerOfToolIcon OnClickListenerWithEditTextToolIcon */ public void setOnClickListenerWithEditTextToolIcon( OnClickListenerWithEditTextToolIcon clickListenerOfToolIcon) { this.onClickListenerWithEditTextToolIcon = clickListenerOfToolIcon; } /** * onTextChange * * @param text text * @param start start * @param lengthBefore lengthBefore * @param lengthAfter lengthAfter */ @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); if (text.length() > 0 && getCompoundDrawables()[2] == null && toolIcon != null) {// hintIcon.setBounds(10, 0, 10, 0); setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, toolIcon, null); } if (text.length() == 0 && getCompoundDrawables()[2] != null && toolIcon != null) { setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); } } /** * interface when click hintIcon */ public interface OnClickListenerWithEditTextHintIcon { void onClick(); } /** * interface when click toolIcon */ public interface OnClickListenerWithEditTextToolIcon { void onClick(); }}

attrs.xml:<?xml version="1.0" encoding="utf-8"?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

android里的editText怎么用,Android自定义控件EditText使用详解相关推荐

  1. Android自定义控件进阶13-MotionEvent详解

    Android MotionEvent 详解,之前用了两篇文章 事件分发机制原理 和 事件分发机制详解 来讲解事件分发,而作为事件分发主角之一的 MotionEvent 并没有过多的说明,本文就带大家 ...

  2. android自定义圆角进度条,Android自定义进度条的圆角横向进度条实例详解

    1.本文将向你介绍自定义进度条的写法,比较简单,但还是有些知识点是需要注意的: invalidate()方法 RectF方法的应用 onMeasure方法的应用 2.原理 画3层圆角矩形,底层为黑色, ...

  3. Android基础入门教程——2.3.1 TextView(文本框)详解

    Android基础入门教程--2.3.1 TextView(文本框)详解 标签(空格分隔): Android基础入门教程 本节引言: 学习完Android中的六大布局,从本节开始我们来一个个讲解And ...

  4. Android四大组件之——Activity的生命周期(图文详解)

        转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai       联系方式:JohnTsai.Work@gmail.com       [Andro ...

  5. android相册和拍照并裁剪图片大小,Android 拍照并对照片进行裁剪和压缩实例详解...

    Android 拍照并对照片进行裁剪和压缩实例详解 本文主要介绍 Android 调用摄像头拍照并对照片进行裁剪和压缩,文中给出了主要步骤和关键代码. 调用摄像头拍照,对拍摄照片进行裁剪,代码如下. ...

  6. Android studio 多渠道(多环境)打包grade配置详解

    Android studio 多渠道(多环境)打包grade配置详解 场景:开发app,我们需要两套环境或者两套环境以上的apk,每套环境的apk分两个版本debug版和release版. 公司有套平 ...

  7. Android Studio CPU profiler性能分析工具介绍和使用详解

    Android Studio CPU profiler性能分析工具介绍和使用详解 CPU profiler介绍 Android Studio CPU 性能剖析器可实时检查应用的 CPU 使用率和线程活 ...

  8. Android Studio打不开虚拟机,两种情况详解

    Android Studio打不开虚拟机,两种情况详解 文章目录 Android Studio打不开虚拟机,两种情况详解 1.VT-x is disabled in BIOS 2.Emulator文件 ...

  9. golang 解析php序列化,golang实现php里的serialize()和unserialize()序列和反序列方法详解...

    Golang 实现 PHP里的 serialize() . unserialize() 安装 go get -u github.com/techleeone/gophp/serialize 用法 pa ...

  10. C语言标准库里的获取时间函数及时间格式转换详解

    C语言标准库里的获取时间函数及时间格式转换详解 头文件: #include <time.h> 相关库函数(截图摘自:https://www.runoob.com/cprogramming/ ...

最新文章

  1. 你写的前端到底用没用到这些
  2. 数据库高级知识——MySql锁机制
  3. SVN 服务器端的搭建-及多仓库管理-OK
  4. 3d建模电脑配置要求_3D建模学习对于电脑配置要求高不高?
  5. 为Exchange Server创建多主机名证书
  6. 故障诊断仪采集发动机EMS故障的报文与故障码记录
  7. 隐马尔可夫链模型学习总结
  8. 百度云如何免费扩容至2055G?
  9. 计算机取证的相关案例,计算机取证案例分析
  10. meethigher-逆向破解今日校园App加密值
  11. dns服务器修改失败,dns错误重新设置方法
  12. Java实现二维码制作
  13. 小米电视刷鸿蒙系统,小米电视怎么刷鸿蒙OS系统?一招立省上千块
  14. 基于hadoop的商品推荐系统_[零基础入门推荐系统(1)]基于用户和基于物品的协同过滤方法(python代码实现)...
  15. Go实战--golang中使用JWT(JSON Web Token)
  16. Unity UGUI Button 中文详解-Chinar
  17. 如何用Sublime Text3 编译和运行Java程序
  18. Android插件化开发指南——Hook技术(一)【长文】
  19. QNX手册学习笔记------同优先级调度算法
  20. 个人入行做智能家居,应该从哪几方面入手?

热门文章

  1. 三种方法实现CSS三栏布局
  2. weblogic反序列化漏洞
  3. Windows XP Service Pack 3 RC2 简体中文版发布
  4. 【★】KMP算法完整教程
  5. C#如何把List of Object转换成List of T具体类型
  6. sqlnet.ora限制导致双机应用资源启动失败
  7. 用再生龙Clonezilla 来克隆Linux
  8. 整理Silverlight资源列表(四)——Silverlight案例补充
  9. top 命令显示隐藏参数列
  10. SQL 2005 Express 的“企业管理器” 下载