溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)帶清除功能的輸入框

發(fā)布時(shí)間:2020-12-07 16:01:49 來源:億速云 閱讀:159 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章給大家分享的是有關(guān)Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)帶清除功能的輸入框,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

一,自定義一個(gè)類,名為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;  
  /** 
   * 控件是否有焦點(diǎn) 
   */ 
  private boolean hasFoucs; 
  
  public ClearEditText(Context context) {  
    this(context, null);  
  }  
  
  public ClearEditText(Context context, AttributeSet attrs) {  
    //這里構(gòu)造方法也很重要,不加這個(gè)很多屬性不能再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,假如沒有設(shè)置我們就使用默認(rèn)的圖片 
    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());  
    //默認(rèn)設(shè)置隱藏圖標(biāo) 
    setClearIconVisible(false);  
    //設(shè)置焦點(diǎn)改變的監(jiān)聽 
    setOnFocusChangeListener(this);  
    //設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽 
    addTextChangedListener(this);  
  }  
  
  
  /** 
   * 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來模擬點(diǎn)擊事件 
   * 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和 
   * EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo),豎直方向就沒有考慮 
   */ 
  @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); 
  } 
  
  /** 
   * 當(dāng)ClearEditText焦點(diǎn)發(fā)生變化的時(shí)候,判斷里面字符串長度設(shè)置清除圖標(biāo)的顯示與隱藏 
   */ 
  @Override  
  public void onFocusChange(View v, boolean hasFocus) {  
    this.hasFoucs = hasFocus; 
    if (hasFocus) {  
      setClearIconVisible(getText().length() > 0);  
    } else {  
      setClearIconVisible(false);  
    }  
  }  
  
  
  /** 
   * 設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去 
   * @param visible 
   */ 
  protected void setClearIconVisible(boolean visible) {  
    Drawable right = visible &#63; mClearDrawable : null;  
    setCompoundDrawables(getCompoundDrawables()[0],  
        getCompoundDrawables()[1], right, getCompoundDrawables()[3]);  
  }  
    
   
  /** 
   * 當(dāng)輸入框里面內(nèi)容發(fā)生變化的時(shí)候回調(diào)的方法 
   */ 
  @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) {  
      
  }  
   
   
  /** 
   * 設(shè)置晃動(dòng)動(dòng)畫 
   */ 
  public void setShakeAnimation(){ 
    this.setAnimation(shakeAnimation(5)); 
  } 
   
   
  /** 
   * 晃動(dòng)動(dòng)畫 
   * @param counts 1秒鐘晃動(dòng)多少下 
   * @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; 
  } 
  
  
} 

里面設(shè)置點(diǎn)擊與輸入的監(jiān)聽的代碼,

setClearIconVisible()方法,設(shè)置隱藏和顯示清除圖標(biāo)的方法,我們這里不是調(diào)用setVisibility()方法,setVisibility()這個(gè)方法是針對(duì)View的,我們可以調(diào)用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)來設(shè)置上下左右的圖標(biāo)

setOnFocusChangeListener(this) 為輸入框設(shè)置焦點(diǎn)改變監(jiān)聽,如果輸入框有焦點(diǎn),我們判斷輸入框的值是否為空,為空就隱藏清除圖標(biāo),否則就顯示

addTextChangedListener(this) 為輸入框設(shè)置內(nèi)容改變監(jiān)聽,其實(shí)很簡單呢,當(dāng)輸入框里面的內(nèi)容發(fā)生改變的時(shí)候,我們需要處理顯示和隱藏清除小圖標(biāo),里面的內(nèi)容長度不為0我們就顯示,否是就隱藏,但這個(gè)需要輸入框有焦點(diǎn)我們才改變顯示或者隱藏,為什么要需要焦點(diǎn),比如我們一個(gè)登陸界面,我們保存了用戶名和密碼,在登陸界面onCreate()的時(shí)候,我們把我們保存的密碼顯示在用戶名輸入框和密碼輸入框里面,輸入框里面內(nèi)容發(fā)生改變,導(dǎo)致用戶名輸入框和密碼輸入框里面的清除小圖標(biāo)都顯示了,這顯然不是我們想要的效果,所以加了一個(gè)是否有焦點(diǎn)的判斷

setShakeAnimation(),這個(gè)方法是輸入框左右抖動(dòng)的方法,之前我在某個(gè)應(yīng)用看到過類似的功能,當(dāng)用戶名錯(cuò)誤,輸入框就在哪里抖動(dòng),感覺挺好玩的,其實(shí)主要是用到一個(gè)移動(dòng)動(dòng)畫,然后設(shè)置動(dòng)畫的變化率為正弦曲線
以上就是Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)帶清除功能的輸入框,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI