Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

自定义View 可清除内容、设置图标、下划线的输入框 MD


目录

案例

<com.bqt.lock.ClearEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:drawableBottom="@drawable/shape_line"
android:drawableLeft="@drawable/icon"
android:maxLines="2"
android:textSize="14sp"/> <com.bqt.lock.ClearEditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:background="#00f"
android:drawableBottom="@drawable/shape_line"
android:drawableLeft="@drawable/icon"
android:drawablePadding="10dp"
android:maxLength="10"
android:maxLines="1"
android:textSize="13sp"/>

ClearEditText

public class ClearEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {

    private Drawable leftIconDrawable;//左侧的一般为输入内容的指示图标
private Drawable bottomLineDrawable;//右侧为清空内容的图标
private Drawable rightClearDrawable;//下侧为下划线
private int size; public ClearEditText(Context context) {
this(context, null);
} public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
} public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
} private void init(Context context) {
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
rightClearDrawable = getCompoundDrawables()[2] == null ? getResources().getDrawable(R.drawable.icon_clean) : getCompoundDrawables()[2];
leftIconDrawable = getCompoundDrawables()[0];
bottomLineDrawable = getCompoundDrawables()[3];
setOnFocusChangeListener(this);
addTextChangedListener(this);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//如果设置了【drawableBottom】,则【drawableRight】的最大高度为【控件的高度 - drawableBottom的高度 - drawablePadding】
int usedHeight = bottomLineDrawable != null ? bottomLineDrawable.getIntrinsicHeight() * 2 + getCompoundDrawablePadding() : 0;
size = getMeasuredHeight() - usedHeight;
Log.i("bqt", "【Drawable的宽高为】" + size);
rightClearDrawable.setBounds(0, 0, size, size);
if (leftIconDrawable != null) {
leftIconDrawable.setBounds(0, 0, size, size);
}
setClearIconVisible(getText().length() > 0 && hasFocus());
} /**
* 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 当我们按下的位置 在 EditText的宽度 -
* 图标到控件右边的间距 - 图标的宽度 和 EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (getCompoundDrawables()[2] != null
&& event.getAction() == MotionEvent.ACTION_UP
&& event.getX() >= (getWidth() - getPaddingRight() - size)
&& (event.getX() <= (getWidth() - getPaddingRight()))) {
this.setText("");
}
return super.onTouchEvent(event);
} /**
* 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
} /**
* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
*/
protected void setClearIconVisible(boolean visible) {
setCompoundDrawables(leftIconDrawable, getCompoundDrawables()[1], visible ? rightClearDrawable : null, bottomLineDrawable);
} @Override
public void onTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void afterTextChanged(Editable s) {
setClearIconVisible(s.length() > 0 && hasFocus());
}
}

下划线

这种方式比单独加一个控件显示分割线更优雅一些

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#D4D4D4"/>
<size
android:width="1000dp"
android:height="1dp"/>
</shape>

2018-10-13

自定义View 可清除内容、设置图标、下划线的输入框 MD的更多相关文章

  1. 2019-10-16-WPF-控件-Content-的内容不显示下划线字符串

    title author date CreateTime categories WPF 控件 Content 的内容不显示下划线字符串 lindexi 2019-10-16 09:21:32 +080 ...

  2. Android-TabLayout设置内容宽度以及下划线宽度

    默认图: 效果图: 项目中使用到需要像今日头条那种实现顶部横向滑动标题功能,本人项目中使用TabLayout+ViewPager实现,但是,实现后默认的TabLayout间距特别大,并且下划线,文字大 ...

  3. NSAttributedString能否设置文字下划线?是否支持line break?

    #import <CoreText/CoreText.h> #import "ViewController.h" @interface ViewController ( ...

  4. 设置TextView下划线并响应点击事件(SpannableString)

    下面是一个20行的完整Demo代码:基本原理是使用一个SpannableString并设置其ClickableSpan来响应点击事件. TextView useInfo = (TextView) fi ...

  5. $Django 多对多-自定义第三张表 基于双下划线的跨表查询(补充)

    自定义第三张表的好处:可以定义多个字段, 缺点:查询不方便(有方法解决) 1.第三张表设置外键,联合唯一(查询不方便) class Books(models.Model): name=models.C ...

  6. 自定义View实现钟摆效果进度条PendulumView

    转载请注明出处:http://blog.csdn.net/fightlei/article/details/52556755 在网上看到了一个IOS组件PendulumView,实现了钟摆的动画效果. ...

  7. 【朝花夕拾】Android自定义View篇之(二)Canvas常用功能

    前言 转在请申明,转自[https://www.cnblogs.com/andy-songwei/p/10960012.html],谢谢! 上一篇讲View的绘制流程中讲到过,最后一步是draw流程, ...

  8. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  9. 利用自定义View实现扫雷游戏

    游戏规则: 简单版的扫雷事实上就是一个9×9的矩阵,其中有十个点是雷,非雷方块的数字代表该方块周围八个方块中雷的个数.通过长按某一方块(方块会变红)认定该方块为玩家认为的雷,通过短按某一方块来“展开” ...

随机推荐

  1. handlebars.min.js的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. BZOJ.2668.[CQOI2012]交换棋子(费用流zkw)

    题目链接 首先黑白棋子的交换等价于黑棋子在白格子图上移动,都到达指定位置. 在这假设我们知道这题用网络流做. 那么黑棋到指定位置就是一条路径,考虑怎么用流模拟出这条路径. 我们发现除了路径的起点和终点 ...

  3. Android activity之间数据传递和共享的方式之Application

    1.基于消息的通信机制  Intent ---bundle ,extra 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream,或者LinkedList链表等等数据类型就不太好用 ...

  4. BZOJ4242 : 水壶

    对于任意两个建筑物,以它们之间的最短路为边权求出最小生成树. 则询问(x,y)的答案为最小生成树上x到y路径上边权的最大值. BFS求出离每个点最近的建筑物以及到它的距离,可以发现只有交界处的边才有用 ...

  5. 2016 Multi-University Training Contest 6 题解

    我只能说: A Boring Question 下面公式重复了一行 \[ \sum\_{0\leq k\_{1},k\_{2},\cdots k\_{m}\leq n}\prod\_{1\leq j& ...

  6. 百度离线下载Tampermonkey脚本

    https://greasyfork.org/zh-CN/scripts/23635-%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98%E7%9B%B4%E6%8E%A5%E4 ...

  7. C# 判断字符编码的六种方法

    方法一http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx在unicode 字符串中,中文的范围是在4E00..9FFF:CJK ...

  8. Microsoft实现的IOC DI之 Unity 、Service Locator、MEF

    这几个工具的站点 Microsoft Unity  http://unity.codeplex.com Service Locator http://commonservicelocator.code ...

  9. 浅谈BFC和IFC

    先说说FC,FC的含义就是Fomatting Context.它是CSS2.1规范中的一个概念. 它是页面中的一块渲染区域.而且有一套渲染规则,它决定了其子元素将怎样定位.以及和其它元素的关系和相互作 ...

  10. HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树/次小生成树)

    题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.如今要在全部城市之间修路,保证每一个城市都能相连,而且保证A/B 最大.全部路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i ...