欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发实现带清空按钮的EditText示例

程序员文章站 2023-11-13 13:51:10
本文实例讲述了android开发实现带清空按钮的edittext。分享给大家供大家参考,具体如下: 一、效果图: 二、具体代码: import andro...

本文实例讲述了android开发实现带清空按钮的edittext。分享给大家供大家参考,具体如下:

一、效果图:

Android开发实现带清空按钮的EditText示例

二、具体代码:

import android.content.context;
import android.graphics.drawable.drawable;
import android.support.v4.content.contextcompat;
import android.support.v4.graphics.drawable.drawablecompat;
import android.support.v7.widget.appcompatedittext;
import android.text.editable;
import android.text.textwatcher;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import com.gdc.control.r;
public class clearableedittext extends appcompatedittext implements view.ontouchlistener, view.onfocuschangelistener, textwatcher {
  private drawable       cleartexticon;
  private onfocuschangelistener monfocuschangelistener;
  private ontouchlistener    montouchlistener;
  private boolean canclear = false;
  public clearableedittext(final context context) {
    super(context);
    init(context);
  }
  public clearableedittext(final context context, final attributeset attrs) {
    super(context, attrs);
    init(context);
  }
  public clearableedittext(final context context, final attributeset attrs,
      final int defstyleattr) {
    super(context, attrs, defstyleattr);
    init(context);
  }
  @override
  public void setonfocuschangelistener(final onfocuschangelistener
      onfocuschangelistener) {
    monfocuschangelistener = onfocuschangelistener;
  }
  @override
  public void setontouchlistener(final ontouchlistener ontouchlistener) {
    montouchlistener = ontouchlistener;
  }
  private void init(final context context) {
    final drawable drawable = contextcompat.getdrawable(context, r.drawable.ic_clear_edittext);
    final drawable wrappeddrawable = drawablecompat.wrap(drawable);
    drawablecompat.settint(wrappeddrawable, getcurrenthinttextcolor());
    cleartexticon = wrappeddrawable;
    cleartexticon.setbounds(0, 0, cleartexticon.getintrinsicwidth(),
        cleartexticon.getintrinsicheight());
    setcleariconvisible(false);
    super.setontouchlistener(this);
    super.setonfocuschangelistener(this);
    addtextchangedlistener(this);
  }
  @override
  public void onfocuschange(final view view, final boolean hasfocus) {
    if (hasfocus) {
      setcleariconvisible(gettext().length() > 0);
    } else {
      setcleariconvisible(false);
      setcanclear(true);
    }
    if (monfocuschangelistener != null) {
      monfocuschangelistener.onfocuschange(view, hasfocus);
    }
  }
  @override
  public boolean ontouch(final view view, final motionevent motionevent) {
    final int x = (int) motionevent.getx();
    if (x > getwidth() - getpaddingright() - cleartexticon.getintrinsicwidth()) {
      if (motionevent.getaction() == motionevent.action_down) {
        if (cleartexticon.isvisible()) {
          seterror(null);
          settext("");
        } else if (iscanclear()) {
          setcanclear(false);
          seterror(null);
          settext("");
        }
      }
      return true;
    } else {
      return montouchlistener != null && montouchlistener.ontouch(view,
          motionevent);
    }
  }
  @override
  public final void ontextchanged(final charsequence s, final int start, final
  int before, final int count) {
    if (isfocused()) {
      setcleariconvisible(s.length() > 0);
    }
  }
  @override
  public void beforetextchanged(charsequence s, int start, int count, int after) {
  }
  @override
  public void aftertextchanged(editable s) {
  }
  private void setcleariconvisible(final boolean visible) {
    cleartexticon.setvisible(visible, false);
    final drawable[] compounddrawables = getcompounddrawables();
    setcompounddrawables(compounddrawables[0], compounddrawables[1], visible ?
        cleartexticon :
        null, compounddrawables[3]);
  }
  public synchronized boolean iscanclear() {
    return canclear;
  }
  public synchronized void setcanclear(boolean canclear) {
    this.canclear = canclear;
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。