自定义EditText实现一键删除数据

转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685

自定义EditText带删除小图标,

实现的功能:

   点击删除小图标,删除当前输入框中所有内容

   删除图标默认不显示,当输入框获得焦点后显示,

实现的操作:

在Edittext的DrawableRight中添加一张删除图标,作为删除功能的小图标

因为Edittext不能为图片设置点击监听事件,因此我们需要自定义Edittext在onTouchEvent方法中模拟按钮点击的操作

当我们触摸抬起(就是ACTION_UP的时候)的范围  大于输入框左侧到清除图标左侧的距离,小与输入框左侧到清除图片右侧的距离,我们则认为是点击清除图片,

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#B0E0E6"
    >

    <TextView
         android:id="@+id/activity_login_ttitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录账号"
        android:textColor="#ffffff"
        android:background="#6699ff"
        android:padding="15dp"
        android:textSize="25sp"
        android:gravity="center"/>
    <ImageView
        android:id="@+id/activity_login_icon"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/activity_login_ttitle"
        android:layout_margin="15dp"/>
  <LinearLayout
      android:id="@+id/activity_login_ll_layout2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center_vertical"
      android:layout_below="@id/activity_login_icon"
      android:padding="15dp"
      >
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:textSize="20sp"
          android:gravity="center_vertical"
          android:textColor="#000000"
          android:text="账 号:"/>
      <com.my.myapp.customeView.CustomeEditext
          android:id="@+id/activity_login_et_user"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textSize="18sp"
          android:singleLine="true"
          android:background="@drawable/edittext_bg_selector"
          android:drawableRight="@drawable/delete_selector"
          android:hint="请输入账号"/>

  </LinearLayout>
    <LinearLayout
        android:id="@+id/activity_login_ll_layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:padding="15dp"
        android:layout_below="@id/activity_login_ll_layout2">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:textColor="#000000"
            android:text="密 码:"/>
        <com.my.myapp.customeView.CustomeEditext
            android:id="@+id/activity_login_et_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="18sp"
            android:inputType="textPassword"
            android:singleLine="true"
            android:background="@drawable/edittext_bg_selector"
            android:drawableRight="@drawable/delete_selector"
            android:hint="请输入密码"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/activity_login_ll_layout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:layout_below="@id/activity_login_ll_layout3">
        <Button
            android:id="@+id/activity_login_btn_login"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="登录"
            android:textSize="18sp"
            android:padding="15dp"
            android:onClick="onLocalLogin"
            android:background="@drawable/btn__login_style_selector"
            />
        <Button
            android:id="@+id/activity_login_register"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="注册"
            android:textSize="18sp"
            android:layout_gravity="right"
            android:layout_marginLeft="30dp"
            android:padding="15dp"
            android:onClick="onLocalLogin"
            android:background="@drawable/btn__login_style_selector"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/activity_login_ll_layout5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:padding="10dp"
        >
        <TextView
            android:id="@+id/activity_login_tv_qq"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="QQ"
            android:gravity="center"
            android:drawableTop="@drawable/ssdk_oks_classic_qq"
            android:layout_margin="5dp"
            android:onClick="onOtherLogin"
            android:clickable="true"/>
        <TextView
            android:id="@+id/activity_login_tv_wechat"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="微信"
            android:gravity="center"
            android:drawableTop="@drawable/ssdk_oks_classic_wechat"
            android:layout_margin="5dp"
            android:onClick="onOtherLogin"
            android:clickable="true"/>
        <TextView
            android:id="@+id/activity_login_tv_email"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="微博"
            android:gravity="center"
            android:layout_margin="5dp"
            android:drawableTop="@drawable/ssdk_oks_classic_tencentweibo"
            android:onClick="onOtherLogin"
            android:clickable="true"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_above="@id/activity_login_ll_layout5"
     >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_centerInParent="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="其他账号登录"
        android:textColor="#CDCDC1"
        android:padding="7dp"
        android:background="#AFEEEE"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:textSize="12dp"
        />
    </RelativeLayout>
</RelativeLayout>

源代码:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;

public class ClearEditText extends EditText implements
        OnFocusChangeListener, TextWatcher {
    /**
     * 删除按钮的引用
     */
    private Drawable mClearDrawable;
    /**
     * 控件是否有焦点
     */
    private boolean hasFoucs;

    public ClearEditText(Context context) {
        this(context, null);
    } 

    public ClearEditText(Context context, AttributeSet attrs) {
        //这里构造方法也很重要,不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle);
    } 

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
        mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {
//            throw new NullPointerException("You can add drawableRight attribute in XML");
            mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
        } 

        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
        //默认设置隐藏图标
        setClearIconVisible(false);
        //设置焦点改变的监听
        setOnFocusChangeListener(this);
        //设置输入框里面内容发生改变的监听
        addTextChangedListener(this);
    } 

    /**
  • setClearIconVisible()方法,设置隐藏和显示清除图标的方法,我们这里不是调用setVisibility()方法,setVisibility()这个方法是针对View的,我们可以调用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)来设置上下左右的图标

  setOnFocusChangeListener(this) 为输入框设置焦点改变监听,如果输入框有焦点,我们判断输入框的值是否为空,为空就隐藏清除图标,否则就显示

  • addTextChangedListener(this) 为输入框设置内容改变监听,其实很简单呢,当输入框里面的内容发生改变的时候,我们需要处理显示和隐藏清除小图标,里面的内容长度不为0我们就显示,否是就隐藏,但这个需要输入框有焦点我们才改变显示或者隐藏,为什么要需要焦点,比如我们一个登陆界面,我们保存了用户名和密码,在登陆界面onCreate()的时候,我们把我们保存的密码显示在用户名输入框和密码输入框里面,输入框里面内容发生改变,导致用户名输入框和密码输入框里面的清除小图标都显示了,这显然不是我们想要的效果,所以加了一个是否有焦点的判断
  • setShakeAnimation(),这个方法是输入框左右抖动的方法,,当用户名错误,输入框就在哪里抖动,感觉挺好玩的,其实主要是用到一个移动动画,然后设置动画的变化率为正弦曲线

效果:

时间: 2024-08-10 19:09:40

自定义EditText实现一键删除数据的相关文章

自定义EditText,带删除按键

带清空按键,还可左右加图片,默认的是加了清空图片. 清空内容是判断的手势点击EditText的范围. 自定义EditText代码如下: import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import androi

登录界面、AutoUtils 屏幕适配、自定义Edittext(显示密码可见和一键清空)和 TextInputLayout的使用。

登录界面: AutoUtils自动屏幕适配: AutoUtils屏幕适配使用的方法 : 1.将AutoUtils类复制到要适配的项目中: 2.在程序的入口(清单文件filter):super.onCreate(savedInstanceState);//屏幕适配,这里是以720*1280分辨率为基准的适配AutoUtils.setSize(this, false, 720, 1280); * 这里我们UI是以1920*1280分辨率做图的,并且是横屏显示:AutoUtils.setSize(th

QGrid系列教程--查询数据、编辑数据、删除数据

查询数据 在ProcessRequest 函数下修改: public void ProcessRequest(HttpContext context) { //查找 if (context.Request.Params.Get("_search") == "true") { return; } NameValueCollection forms = context.Request.Form; string strOperation = forms.Get("

Android 自定义EditText输入框 带清空按钮

总结  Android 自定义EditText输入框 带清空按钮 当用户输入字符后  EditText会自动在输入框的内部右侧出现删除按钮 重写EditText达到简化布局的效果 效果图: 继承EditText package com.example.myedittexttest; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; imp

sharepoint 2016 学习系列篇(15)-自定义列表应用篇-(4)数据权限配置

当数据已经录入到列表中之后,接下来,朋友们可能会想知道,有些数据,只想给某些用户看到,或者编辑,列表是否支持这样的操作. 大微软的sharepoint平台,对于用户的需求,可以说是考虑得很周全的,权限管理,当然也是不可能漏掉的,而且权限管理,还是sharepoint上一个非常突出的亮点,数据安全性管理的颗粒度,很完善.接下来,我们来看下,如何在sharepoint的自定义列表中,给数据配置不同的用户访问权限,这些当然也是不需要开发,就能实现的. 前面讲到,用户访问sharepoint平台网站的时

python实现人人网留言获取与一键删除

最近有点着迷Python,学习基本语法之后,首先从爬虫开始,看了<使用python登录人人网并发表状态>一文后,很感兴趣,然后又曾经苦于人人网聊天记录删除的繁琐,于是决定写一个聊天记录一键删除的小脚本,好啦,废话不多说: #encoding:utf-8 import urllib2, urllib, cookielib, re, string class spider(): def __init__(self, email, password): self.email = email self

Web jquery表格组件 JQGrid 的使用 - 7.查询数据、编辑数据、删除数据

系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件及方法 Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页.搜索.格式化.自定义按钮 Web jquery表格组件 JQGrid 的使用 - 6.准备工作 & Hello JQGrid Web jquery表格组件 JQGrid 的使用 - 7.查询数据.编辑数据.删除数据

自定义EditText动态控制输入的字符数量

在开发中难免会遇到要控制字符数量的输入限制,比如用户的名字会让你输入10个字符(意思是英文可以输入10个但是汉字只可以输入5个),也许第一反应会在xml中设置EditText的长度,这种情况肯定是达不到我们的要求的.下面介绍一下自己的方法. 1.声明一个类继承InputFilter,实现filter中的方法,下面有详细的注解读者可以仔细看下 public class MyLenghtFilter implements InputFilter { int nMax=0; int keep=0; p

Android 带清除 和 晃动效果的 自定义 EditText 解析

转载请标明出处 :http://blog.csdn.net/qq_19986309 尊重他人劳动成果~ 谢谢 今天给大家实现一个 带清除按钮  和 晃动效果的 自定义 EditText 常常大家写登录 注册按钮 可能是需要用到的, 好了 废话不多说 直接上效果图 这样的效果还是不错的  也是比较实用 不管在哪里的注册登录都用得着 下面贴下代码 public class ClearWriteEditText extends EditText implements View.OnFocusChang