欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Android實現(xiàn)自定義帶刪除功能的EditText實例

1.說明

成都創(chuàng)新互聯(lián)公司是一家專注于網站制作、網站設計與策劃設計,溫泉網站建設哪家好?成都創(chuàng)新互聯(lián)公司做網站,專注于網站建設十載,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:溫泉等地區(qū)。溫泉做網站價格咨詢:18980820575

自定義帶刪除功能的EditText有兩種方法,第一種是用組合視圖的方法,即在一個view視圖里面左側放置一個EditText,右側放置一個ImageView,但是這樣增加了視圖的層次,而且對輸入內容的長度要做一定的處理。

第二種是重新定義EditText組件,增加相應的事件處理,即可達到很好的效果,效果圖如下:

Android實現(xiàn)自定義帶刪除功能的EditText實例

2.ClearEditText的JAVA類文件

/** 
 * @說明: 自定義帶刪除按鈕的EditText 
 * 
 */ 
public class ClearEditText extends EditText implements OnFocusChangeListener, 
    TextWatcher { 
  //EditText右側的刪除按鈕 
  private Drawable mClearDrawable; 
  private boolean hasFoucs; 
 
  public ClearEditText(Context context) { 
    this(context, null); 
  } 
 
  public ClearEditText(Context context, AttributeSet attrs) { 
    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,假如沒有設置我們就使用默認的圖片,獲取圖片的順序是左上右下(0,1,2,3,) 
    mClearDrawable = getCompoundDrawables()[2]; 
    if (mClearDrawable == null) { 
      mClearDrawable = getResources().getDrawable( 
          R.drawable.edit_delete); 
    } 
 
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), 
        mClearDrawable.getIntrinsicHeight()); 
    // 默認設置隱藏圖標 
    setClearIconVisible(false); 
    // 設置焦點改變的監(jiān)聽 
    setOnFocusChangeListener(this); 
    // 設置輸入框里面內容發(fā)生改變的監(jiān)聽 
    addTextChangedListener(this); 
  } 
     
  /* @說明:isInnerWidth, isInnerHeight為ture,觸摸點在刪除圖標之內,則視為點擊了刪除圖標 
   * event.getX() 獲取相對應自身左上角的X坐標 
   * event.getY() 獲取相對應自身左上角的Y坐標 
   * getWidth() 獲取控件的寬度 
   * getHeight() 獲取控件的高度 
   * getTotalPaddingRight() 獲取刪除圖標左邊緣到控件右邊緣的距離 
   * getPaddingRight() 獲取刪除圖標右邊緣到控件右邊緣的距離 
   * isInnerWidth: 
   * getWidth() - getTotalPaddingRight() 計算刪除圖標左邊緣到控件左邊緣的距離 
   * getWidth() - getPaddingRight() 計算刪除圖標右邊緣到控件左邊緣的距離 
   * isInnerHeight: 
   * distance 刪除圖標頂部邊緣到控件頂部邊緣的距離 
   * distance + height 刪除圖標底部邊緣到控件頂部邊緣的距離 
   */ 
  @Override 
  public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (getCompoundDrawables()[2] != null) { 
        int x = (int)event.getX(); 
        int y = (int)event.getY(); 
        Rect rect = getCompoundDrawables()[2].getBounds(); 
        int height = rect.height(); 
        int distance = (getHeight() - height)/2; 
        boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight()); 
        boolean isInnerHeight = y > distance && y <(distance + height); 
        if (isInnerWidth && isInnerHeight) { 
          this.setText(""); 
        } 
      } 
    } 
    return super.onTouchEvent(event); 
  } 
 
  /** 
   * 當ClearEditText焦點發(fā)生變化的時候, 
   * 輸入長度為零,隱藏刪除圖標,否則,顯示刪除圖標 
   */ 
  @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) { 
 
  } 
   
 
} 

3.引用ClearEditText的XML文件

<com.once.android_ui.selfview.ClearEditText 
    android:id="@+id/user_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/user_name" 
    android:drawablePadding="7dp" 
    android:hint="@string/name_tip" 
    android:singleLine="true" 
    android:textSize="17sp" > 
    <requestFocus /> 
  </com.once.android_ui.selfview.ClearEditText> 

附件是圖片資源文件。


Android實現(xiàn)自定義帶刪除功能的EditText實例Android實現(xiàn)自定義帶刪除功能的EditText實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標題:Android實現(xiàn)自定義帶刪除功能的EditText實例
文章地址:http://aaarwkj.com/article4/gjojie.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站改版、、網站設計公司、網頁設計公司、商城網站、Google

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站網頁設計
欧美激情另类综合国内| 蜜桃福利视频一区二区| 亚洲av最近在线观看| 中文字幕欧美精品日韩人妻| 天堂av免费资源在线观看| 伊人激情一区二区三区| 精精国产xxxx视频在线不卡| av一区二区三区网站| 日本一区二区三区高清在线| 欧美香蕉高清视频免费| 99久热在线精品视频| 日韩一级久久精品理论| 亚洲不卡一区二区在线| 日本一区二区三区免费看视频| 国产伦理自拍视频在线观看| 九色91成人在线视频| 97公开视频在线观看| 青春草草视频在线观看| 91九色国产老熟女乱子| 熟女少妇久久中文字幕| 国产日韩亚洲欧美精品专区| 亚洲一区二区精品欧美日韩| 欧美日韩亚洲一区二区搜索| 日韩欧美一区二区麻豆| 午夜射精视频在线观看| 黄色高清无遮挡在线观看| 日韩午夜电影一区二区三区| 色综合久久国产原创野外| 欧美亚洲另类国产精品| 最新免费观看男女啪啪视频| 精品亚洲第一区二区免费在线 | 日本熟女午夜福利视频| 蜜臀av人妻一区二区三区| 神马久久午夜免费福利| 久久99久久久国产精品| 亚洲男人天堂日本一区| 亚洲欧美国产精品日韩| 日本高清加勒比免费在线| 国产精品久久久av大片| 欧美日韩亚洲人人夜夜澡| 一区二区高清中文字幕|