栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现自定义带删除功能的EditText实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现自定义带删除功能的EditText实例

1.说明

自定义带删除功能的EditText有两种方法,第一种是用组合视图的方法,即在一个view视图里面左侧放置一个EditText,右侧放置一个ImageView,但是这样增加了视图的层次,而且对输入内容的长度要做一定的处理。

第二种是重新定义EditText组件,增加相应的事件处理,即可达到很好的效果,效果图如下:

2.ClearEditText的JAVA类文件

 
public class ClearEditText extends EditText implements OnFocusChangeListener, 
    TextWatcher { 
  //EditText右侧的删除按钮 
  private Drawable mClearDrawable; 
  private boolean hasFoucs; 
 
  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(); 
  } 
 
  private void init() { 
    // 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,获取图片的顺序是左上右下(0,1,2,3,) 
    mClearDrawable = getCompoundDrawables()[2]; 
    if (mClearDrawable == null) { 
      mClearDrawable = getResources().getDrawable( 
   R.drawable.edit_delete); 
    } 
 
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), 
 mClearDrawable.getIntrinsicHeight()); 
    // 默认设置隐藏图标 
    setClearIconVisible(false); 
    // 设置焦点改变的监听 
    setonFocusChangeListener(this); 
    // 设置输入框里面内容发生改变的监听 
    addTextChangedListener(this); 
  } 
     
   
  @Override 
  public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (getCompoundDrawables()[2] != null) { 
 int x = (int)event.getX(); 
 int y = (int)event.getY(); 
 Rect rect = getCompoundDrawables()[2].getBounds(); 
 int height = rect.height(); 
 int distance = (getHeight() - height)/2; 
 boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight()); 
 boolean isInnerHeight = y > distance && y <(distance + height); 
 if (isInnerWidth && isInnerHeight) { 
   this.setText(""); 
 } 
      } 
    } 
    return super.onTouchEvent(event); 
  } 
 
   
  @Override 
  public void onFocusChange(View v, boolean hasFocus) { 
    this.hasFoucs = hasFocus; 
    if (hasFocus) { 
      setClearIconVisible(getText().length() > 0); 
    } else { 
      setClearIconVisible(false); 
    } 
  } 
 
  protected void setClearIconVisible(boolean visible) { 
    Drawable right = visible ? mClearDrawable : null; 
    setCompoundDrawables(getCompoundDrawables()[0], 
 getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 
  } 
 
  @Override 
  public void onTextChanged(CharSequence s, int start, int count, int after) { 
    if (hasFoucs) { 
      setClearIconVisible(s.length() > 0); 
    } 
  } 
 
  @Override 
  public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
 
  } 
 
  @Override 
  public void afterTextChanged(Editable s) { 
 
  } 
   
 
} 

3.引用ClearEditText的XML文件

 
     
   

附件是图片资源文件。



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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号