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

Android 带清除功能的输入框控件实例详解

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

Android 带清除功能的输入框控件实例详解

Android 带清除功能的输入框控件实例详解

今天,看到一个很好的自定义输入框控件,于是记录一下。

效果很好:

一,自定义一个类,名为ClearEditText

package com.example.clearedittext; 
 
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);  
  }  
  
  
   
  @Override  
  public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (getCompoundDrawables()[2] != null) { 
 
 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) 
     && (event.getX() < ((getWidth() - getPaddingRight()))); 
  
 if (touchable) { 
   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) {  
      
  }  
   
   
   
  public void setShakeAnimation(){ 
    this.setAnimation(shakeAnimation(5)); 
  } 
   
   
   
  public static Animation shakeAnimation(int counts){ 
    Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); 
    translateAnimation.setInterpolator(new CycleInterpolator(counts)); 
    translateAnimation.setDuration(1000); 
    return translateAnimation; 
  } 
  
  
} 

里面设置点击与输入的监听的代码,

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

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

addTextChangedListener(this) 为输入框设置内容改变监听,其实很简单呢,当输入框里面的内容发生改变的时候,我们需要处理显示和隐藏清除小图标,里面的内容长度不为0我们就显示,否是就隐藏,但这个需要输入框有焦点我们才改变显示或者隐藏,为什么要需要焦点,比如我们一个登陆界面,我们保存了用户名和密码,在登陆界面onCreate()的时候,我们把我们保存的密码显示在用户名输入框和密码输入框里面,输入框里面内容发生改变,导致用户名输入框和密码输入框里面的清除小图标都显示了,这显然不是我们想要的效果,所以加了一个是否有焦点的判断

setShakeAnimation(),这个方法是输入框左右抖动的方法,之前我在某个应用看到过类似的功能,当用户名错误,输入框就在哪里抖动,感觉挺好玩的,其实主要是用到一个移动动画,然后设置动画的变化率为正弦曲线
然后直接在布局文件使用就可以了。使用的效果


Android 带清除功能的输入框控件就讲完了。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

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

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

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