最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義控件EditText實(shí)現(xiàn)清除和抖動(dòng)功能

 更新時(shí)間:2017年12月07日 14:12:21   作者:難不難太難了  
這篇文章主要為大家詳細(xì)介紹了Android自定義控件EditText實(shí)現(xiàn)清除和抖動(dòng)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android EditText實(shí)現(xiàn)清除和抖動(dòng)功能的具體代碼,供大家參考,具體內(nèi)容如下

源碼如下:

public class ClearEditText extends EditText implements View.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,假如沒(méi)有設(shè)置我們就使用默認(rèn)的圖片,2是獲得右邊的圖片 順序是左上右下(0,1,2,3,) 
  mClearDrawable = getCompoundDrawables()[2]; 
  if (mClearDrawable == null) { 
   // throw new 
   // NullPointerException("You can add drawableRight attribute in XML"); 
   mClearDrawable = getResources().getDrawable(R.drawable.icon_clear_input); 
  } 
 
  mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(),mClearDrawable.getIntrinsicHeight()); 
  // 默認(rèn)設(shè)置隱藏圖標(biāo) 
  setClearIconVisible(false); 
  // 設(shè)置焦點(diǎn)改變的監(jiān)聽(tīng) 
  setOnFocusChangeListener(this); 
  // 設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽(tīng) 
  addTextChangedListener(this); 
 } 
 
 / 
  * 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來(lái)模擬點(diǎn)擊事件 當(dāng)我們按下的位置 在 EditText的寬度 - 
  * 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和 EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo),豎直方向就沒(méi)有考慮 
  */ 
 @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í)候,判斷里面字符串長(zhǎng)度設(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 ? 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)畫(huà) 
  */ 
 public void setShakeAnimation() { 
  this.startAnimation(shakeAnimation(5)); 
 } 
 
 / 
  * 晃動(dòng)動(dòng)畫(huà) 
  * 
  * @param counts 
  *   1秒鐘晃動(dòng)多少下 
  * @return 
  */ 
 public static Animation shakeAnimation(int counts) { 
  Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); 
  //設(shè)置一個(gè)循環(huán)加速器,使用傳入的次數(shù)就會(huì)出現(xiàn)擺動(dòng)的效果。 
  translateAnimation.setInterpolator(new CycleInterpolator(counts)); 
  translateAnimation.setDuration(500); 
  return translateAnimation; 
 } 
 
} 

使用方法同普通的EditText:

<com.example.clearedittext.ClearEditText  
    android:id="@+id/username"  
    android:layout_marginTop="60dp"  
    android:layout_width="fill_parent"  
    android:background="@drawable/login_edittext_bg"   
    android:drawableLeft="@drawable/icon_user"  
    android:layout_marginLeft="10dip"  
    android:layout_marginRight="10dip"  
    android:singleLine="true"  
    android:drawableRight="@drawable/delete_selector"  
    android:hint="輸入用戶(hù)名"  
    android:layout_height="wrap_content" /> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢(xún)

    淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢(xún)

    這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢(xún)的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • Android實(shí)現(xiàn)簡(jiǎn)單用戶(hù)注冊(cè)案例

    Android實(shí)現(xiàn)簡(jiǎn)單用戶(hù)注冊(cè)案例

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單用戶(hù)注冊(cè)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android圖片三級(jí)緩存開(kāi)發(fā)

    Android圖片三級(jí)緩存開(kāi)發(fā)

    這篇文章主要為大家詳細(xì)介紹了Android圖片三級(jí)緩存開(kāi)發(fā)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android ProgressDialog使用總結(jié)

    Android ProgressDialog使用總結(jié)

    ProgressDialog 繼承自AlertDialog,AlertDialog繼承自Dialog,實(shí)現(xiàn)DialogInterface接口,本文給大家介紹Android ProgressDialog使用總結(jié)的相關(guān)知識(shí),需要的朋友通過(guò)此文一起學(xué)習(xí)吧
    2016-01-01
  • Android 是如何捕捉 java 異常的

    Android 是如何捕捉 java 異常的

    這篇文章主要介紹Android 是如何捕捉 java 異常的,他是怎么實(shí)現(xiàn)全局捕捉的,帶著這樣的疑問(wèn)和小編一起閱讀下面文章的具體內(nèi)容吧
    2021-09-09
  • Android shape 繪制圖形的實(shí)例詳解

    Android shape 繪制圖形的實(shí)例詳解

    這篇文章主要介紹了Android shape 繪制圖形的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Android Jetpack系列之App Startup使用詳解

    Android Jetpack系列之App Startup使用詳解

    這篇文章主要為大家介紹了Android Jetpack系列之App Startup使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 模擬按Home鍵退出應(yīng)用的簡(jiǎn)單方法(分享)

    模擬按Home鍵退出應(yīng)用的簡(jiǎn)單方法(分享)

    下面小編就為大家?guī)?lái)一篇模擬按Home鍵退出應(yīng)用的簡(jiǎn)單方法(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 使用Messenger實(shí)現(xiàn)Service的雙向通信

    使用Messenger實(shí)現(xiàn)Service的雙向通信

    這篇文章主要為大家詳細(xì)介紹了使用Messenger實(shí)現(xiàn)Service的雙向通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Andriod?Studio實(shí)現(xiàn)撥打電話(huà)和發(fā)送短信的示例代碼

    Andriod?Studio實(shí)現(xiàn)撥打電話(huà)和發(fā)送短信的示例代碼

    這篇文章主要介紹了Andriod?Studio實(shí)現(xiàn)撥打電話(huà)和發(fā)送短信功能,Android?Studio中創(chuàng)建項(xiàng)目,然后在該項(xiàng)目中創(chuàng)建一個(gè)Module名稱(chēng)為“IntentDial”,文章結(jié)合實(shí)例步驟給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-03-03

最新評(píng)論

安宁市| 江门市| 金门县| 凉城县| 陆河县| 呈贡县| 石林| 朝阳市| 盘锦市| 庄河市| 登封市| 娄底市| 莱芜市| 凉城县| 太白县| 汉中市| 开平市| 武平县| 清苑县| 乌兰浩特市| 陇南市| 鱼台县| 界首市| 元阳县| 太湖县| 大渡口区| 密山市| 新源县| 高平市| 郸城县| 龙川县| 略阳县| 都安| 淳安县| 沅江市| 舟曲县| 乳源| 马边| 丰顺县| 南宁市| 石阡县|