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

Android 带有删除按钮的EditText

程序员文章站 2024-03-05 08:59:48
mainactivity如下: package cc.textview5; import android.os.bundle; import an...

Android 带有删除按钮的EditText

mainactivity如下:

package cc.textview5; 
import android.os.bundle; 
import android.text.textutils; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.widget.button; 
import android.widget.toast; 
import android.app.activity; 
/** 
 * demo描述: 
 * 自定义控件实现带清除功能的edittext 
 * 
 * 学习资料: 
 * http://blog.csdn.net/xiaanming/article/details/11066685 
 * 
 * thank you very much 
 */ 
public class mainactivity extends activity { 
  private cleanableedittext musernamecleanableedittext; 
  private cleanableedittext mpasswordcleanableedittext; 
  private button mloginbutton; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    init(); 
  } 
   
  private void init(){ 
    musernamecleanableedittext=(cleanableedittext) findviewbyid(r.id.usernameedittext); 
    mpasswordcleanableedittext=(cleanableedittext) findviewbyid(r.id.passwordedittext); 
    mloginbutton=(button) findviewbyid(r.id.loginbutton); 
    mloginbutton.setonclicklistener(new onclicklistenerimpl()); 
  } 
   
  private class onclicklistenerimpl implements onclicklistener { 
    @override 
    public void onclick(view view) { 
      if (textutils.isempty(musernamecleanableedittext.gettext())) { 
        musernamecleanableedittext.setshakeanimation(); 
        toast.maketext(mainactivity.this, "请输入用户名", toast.length_short).show(); 
      } 
 
      if (textutils.isempty(mpasswordcleanableedittext.gettext())) { 
        mpasswordcleanableedittext.setshakeanimation(); 
        toast.maketext(mainactivity.this, "请输入密码", toast.length_short).show(); 
      } 
    } 
 
  } 
   
 
} 

cleanableedittext如下:

package cc.textview5; 
 
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.animation.animation; 
import android.view.animation.cycleinterpolator; 
import android.view.animation.translateanimation; 
import android.widget.edittext; 
/** 
 * 在焦点变化时和输入内容发生变化时均要判断是否显示右边clean图标 
 */ 
public class cleanableedittext extends edittext { 
  private drawable mrightdrawable; 
  private boolean ishasfocus; 
   
  public cleanableedittext(context context) { 
    super(context); 
    init(); 
  } 
  public cleanableedittext(context context, attributeset attrs) { 
    super(context, attrs); 
    init(); 
  } 
 
  public cleanableedittext(context context, attributeset attrs, int defstyle) { 
    super(context, attrs, defstyle); 
    init(); 
  } 
   
  private void init(){ 
    //getcompounddrawables: 
    //returns drawables for the left, top, right, and bottom borders. 
    drawable [] drawables=this.getcompounddrawables(); 
     
    //取得right位置的drawable 
    //即我们在布局文件中设置的android:drawableright 
    mrightdrawable=drawables[2];   
     
    //设置焦点变化的监听 
    this.setonfocuschangelistener(new focuschangelistenerimpl()); 
    //设置edittext文字变化的监听 
    this.addtextchangedlistener(new textwatcherimpl()); 
    //初始化时让右边clean图标不可见 
    setcleardrawablevisible(false); 
  } 
   
   
  /** 
   * 当手指抬起的位置在clean的图标的区域 
   * 我们将此视为进行清除操作 
   * getwidth():得到控件的宽度 
   * event.getx():抬起时的坐标(改坐标是相对于控件本身而言的) 
   * gettotalpaddingright():clean的图标左边缘至控件右边缘的距离 
   * getpaddingright():clean的图标右边缘至控件右边缘的距离 
   * 于是: 
   * getwidth() - gettotalpaddingright()表示: 
   * 控件左边到clean的图标左边缘的区域 
   * getwidth() - getpaddingright()表示: 
   * 控件左边到clean的图标右边缘的区域 
   * 所以这两者之间的区域刚好是clean的图标的区域 
   */ 
  @override 
  public boolean ontouchevent(motionevent event) { 
    switch (event.getaction()) { 
    case motionevent.action_up: 
       
      boolean isclean =(event.getx() > (getwidth() - gettotalpaddingright()))&& 
               (event.getx() < (getwidth() - getpaddingright())); 
      if (isclean) { 
        settext(""); 
      } 
      break; 
 
    default: 
      break; 
    } 
    return super.ontouchevent(event); 
  } 
   
  private class focuschangelistenerimpl implements onfocuschangelistener{ 
    @override 
    public void onfocuschange(view v, boolean hasfocus) { 
       ishasfocus=hasfocus; 
       if (ishasfocus) { 
         boolean isvisible=gettext().tostring().length()>=1; 
         setcleardrawablevisible(isvisible); 
      } else { 
         setcleardrawablevisible(false); 
      } 
    } 
     
  } 
   
  //当输入结束后判断是否显示右边clean的图标 
  private class textwatcherimpl implements textwatcher{ 
    @override 
    public void aftertextchanged(editable s) { 
       boolean isvisible=gettext().tostring().length()>=1; 
       setcleardrawablevisible(isvisible); 
    } 
 
    @override 
    public void beforetextchanged(charsequence s, int start, int count,int after) { 
       
    } 
 
    @override 
    public void ontextchanged(charsequence s, int start, int before,int count) { 
       
    } 
     
  }   
   
  //隐藏或者显示右边clean的图标 
  protected void setcleardrawablevisible(boolean isvisible) { 
    drawable rightdrawable; 
    if (isvisible) { 
      rightdrawable = mrightdrawable; 
    } else { 
      rightdrawable = null; 
    } 
    //使用代码设置该控件left, top, right, and bottom处的图标 
    setcompounddrawables(getcompounddrawables()[0],getcompounddrawables()[1],  
               rightdrawable,getcompounddrawables()[3]); 
  }  
 
  // 显示一个动画,以提示用户输入 
  public void setshakeanimation() { 
    this.setanimation(shakeanimation(5)); 
  } 
 
  //cycletimes动画重复的次数 
  public animation shakeanimation(int cycletimes) { 
    animation translateanimation = new translateanimation(0, 10, 0, 10); 
    translateanimation.setinterpolator(new cycleinterpolator(cycletimes)); 
    translateanimation.setduration(1000); 
    return translateanimation; 
  } 
 
} 

main.xml如下:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingleft="20dip" 
  android:paddingright="20dip" 
  > 
 
  <cc.textview5.cleanableedittext 
    android:id="@+id/usernameedittext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="username" 
    android:layout_margintop="30dip" 
    android:drawableleft="@drawable/icon_user" 
    android:drawableright="@drawable/clean_selector" 
     /> 
   
   <cc.textview5.cleanableedittext 
    android:id="@+id/passwordedittext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="password" 
    android:password="true" 
    android:layout_margintop="100dip" 
    android:drawableleft="@drawable/account_icon" 
    android:drawableright="@drawable/clean_selector" 
     /> 
    
   <button 
     android:id="@+id/loginbutton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="login" 
    android:layout_margintop="155dip" 
     /> 
 
</relativelayout> 





以上就是对android   带有删除按钮的edittext的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!