Editiew 有刪除圖標 和焦點判斷

  1. public class ClearEditText extends EditText implements    
  2.         OnFocusChangeListener, TextWatcher {   
  3.     /** 
  4.      * 刪除按鈕的引用 
  5.      */  
  6.     private Drawable mClearDrawable;   
  7.     /** 
  8.      * 控件是否有焦點 
  9.      */  
  10.     private boolean hasFoucs;  
  11.    
  12.     public ClearEditText(Context context) {   
  13.         this(context, null);   
  14.     }   
  15.    
  16.     public ClearEditText(Context context, AttributeSet attrs) {   
  17.         //這裏構造方法也很重要,不加這個很多屬性不能再XML裏面定義  
  18.         this(context, attrs, android.R.attr.editTextStyle);   
  19.     }   
  20.       
  21.     public ClearEditText(Context context, AttributeSet attrs, int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         init();  
  24.     }  
  25.       
  26.       
  27.     private void init() {   
  28.         //獲取EditText的DrawableRight,假如沒有設置我們就使用默認的圖片  
  29.         mClearDrawable = getCompoundDrawables()[2];   
  30.         if (mClearDrawable == null) {   
  31. //          throw new NullPointerException("You can add drawableRight attribute in XML");  
  32.             mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);   
  33.         }   
  34.           
  35.         mClearDrawable.setBounds(00, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());   
  36.         //默認設置隱藏圖標  
  37.         setClearIconVisible(false);   
  38.         //設置焦點改變的監聽  
  39.         setOnFocusChangeListener(this);   
  40.         //設置輸入框裏面內容發生改變的監聽  
  41.         addTextChangedListener(this);   
  42.     }   
  43.    
  44.    
  45.     /** 
  46.      * 因爲我們不能直接給EditText設置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件 
  47.      * 當我們按下的位置 在  EditText的寬度 - 圖標到控件右邊的間距 - 圖標的寬度  和 
  48.      * EditText的寬度 - 圖標到控件右邊的間距之間我們就算點擊了圖標,豎直方向就沒有考慮 
  49.      */  
  50.     @Override   
  51.     public boolean onTouchEvent(MotionEvent event) {  
  52.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  53.             if (getCompoundDrawables()[2] != null) {  
  54.   
  55.                 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())  
  56.                         && (event.getX() < ((getWidth() - getPaddingRight())));  
  57.                   
  58.                 if (touchable) {  
  59.                     this.setText("");  
  60.                 }  
  61.             }  
  62.         }  
  63.   
  64.         return super.onTouchEvent(event);  
  65.     }  
  66.    
  67.     /** 
  68.      * 當ClearEditText焦點發生變化的時候,判斷裏面字符串長度設置清除圖標的顯示與隱藏 
  69.      */  
  70.     @Override   
  71.     public void onFocusChange(View v, boolean hasFocus) {   
  72.         this.hasFoucs = hasFocus;  
  73.         if (hasFocus) {   
  74.             setClearIconVisible(getText().length() > 0);   
  75.         } else {   
  76.             setClearIconVisible(false);   
  77.         }   
  78.     }   
  79.    
  80.    
  81.     /** 
  82.      * 設置清除圖標的顯示與隱藏,調用setCompoundDrawables爲EditText繪製上去 
  83.      * @param visible 
  84.      */  
  85.     protected void setClearIconVisible(boolean visible) {   
  86.         Drawable right = visible ? mClearDrawable : null;   
  87.         setCompoundDrawables(getCompoundDrawables()[0],   
  88.                 getCompoundDrawables()[1], right, getCompoundDrawables()[3]);   
  89.     }   
  90.        
  91.       
  92.     /** 
  93.      * 當輸入框裏面內容發生變化的時候回調的方法 
  94.      */  
  95.     @Override   
  96.     public void onTextChanged(CharSequence s, int start, int count,   
  97.             int after) {   
  98.                 if(hasFoucs){  
  99.                     setClearIconVisible(s.length() > 0);  
  100.                 }  
  101.     }   
  102.    
  103.     @Override   
  104.     public void beforeTextChanged(CharSequence s, int start, int count,   
  105.             int after) {   
  106.            
  107.     }   
  108.    
  109.     @Override   
  110.     public void afterTextChanged(Editable s) {   
  111.            
  112.     }   
  113.       
  114.      
  115.     /** 
  116.      * 設置晃動動畫 
  117.      */  
  118.     public void setShakeAnimation(){  
  119.         this.setAnimation(shakeAnimation(5));  
  120.     }  
  121.       
  122.       
  123.     /** 
  124.      * 晃動動畫 
  125.      * @param counts 1秒鐘晃動多少下 
  126.      * @return 
  127.      */  
  128.     public static Animation shakeAnimation(int counts){  
  129.         Animation translateAnimation = new TranslateAnimation(01000);  
  130.         translateAnimation.setInterpolator(new CycleInterpolator(counts));  
  131.         translateAnimation.setDuration(1000);  
  132.         return translateAnimation;  
  133.     }  
  134.    
  135.    
  136. }  








xml 佈局  
 <com.example.test.ClearEditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="26dp"
        android:ems="10" >

        <requestFocus />
    </com.example.test.ClearEditText>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章