国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 帶清除功能的輸入框控件實例詳解

2019-10-23 18:27:23
字體:
來源:轉載
供稿:網友

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);    }        /**    * 因為我們不能直接給EditText設置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件    * 當我們按下的位置 在 EditText的寬度 - 圖標到控件右邊的間距 - 圖標的寬度 和    * EditText的寬度 - 圖標到控件右邊的間距之間我們就算點擊了圖標,豎直方向就沒有考慮    */   @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);   }     /**    * 當ClearEditText焦點發生變化的時候,判斷里面字符串長度設置清除圖標的顯示與隱藏    */   @Override    public void onFocusChange(View v, boolean hasFocus) {      this.hasFoucs = hasFocus;     if (hasFocus) {        setClearIconVisible(getText().length() > 0);      } else {        setClearIconVisible(false);      }    }        /**    * 設置清除圖標的顯示與隱藏,調用setCompoundDrawables為EditText繪制上去    * @param visible    */   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));   }         /**    * 晃動動畫    * @param counts 1秒鐘晃動多少下    * @return    */   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,帶清除功能的輸入框控件,實現清除功能的輸入框

Android 帶清除功能的輸入框控件就講完了。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清镇市| 奇台县| 义马市| 苍山县| 耿马| 上高县| 濮阳县| 新竹县| 全椒县| 堆龙德庆县| 巴彦淖尔市| 福泉市| 永川市| 合肥市| 安吉县| 玛纳斯县| 攀枝花市| 随州市| 南投市| 永康市| 六盘水市| 景德镇市| 莱芜市| 翁源县| 开江县| 英超| 临海市| 铜鼓县| 湟源县| 萨迦县| 靖州| 老河口市| 安图县| 开封市| 迁安市| 成武县| 封丘县| 朝阳市| 封丘县| 梅河口市| 罗田县|