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

Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText示例

 更新時(shí)間:2017年11月21日 12:00:55   作者:勤修戒定慧  
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)EditText清空按鈕功能相關(guān)操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText。分享給大家供大家參考,具體如下:

一、效果圖:

二、具體代碼:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.gdc.control.R;
public class ClearableEditText extends AppCompatEditText implements View.OnTouchListener, View.OnFocusChangeListener, TextWatcher {
  private Drawable       clearTextIcon;
  private OnFocusChangeListener mOnFocusChangeListener;
  private OnTouchListener    mOnTouchListener;
  private boolean canClear = false;
  public ClearableEditText(final Context context) {
    super(context);
    init(context);
  }
  public ClearableEditText(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }
  public ClearableEditText(final Context context, final AttributeSet attrs,
      final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }
  @Override
  public void setOnFocusChangeListener(final OnFocusChangeListener
      onFocusChangeListener) {
    mOnFocusChangeListener = onFocusChangeListener;
  }
  @Override
  public void setOnTouchListener(final OnTouchListener onTouchListener) {
    mOnTouchListener = onTouchListener;
  }
  private void init(final Context context) {
    final Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_clear_edittext);
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
    clearTextIcon = wrappedDrawable;
    clearTextIcon.setBounds(0, 0, clearTextIcon.getIntrinsicWidth(),
        clearTextIcon.getIntrinsicHeight());
    setClearIconVisible(false);
    super.setOnTouchListener(this);
    super.setOnFocusChangeListener(this);
    addTextChangedListener(this);
  }
  @Override
  public void onFocusChange(final View view, final boolean hasFocus) {
    if (hasFocus) {
      setClearIconVisible(getText().length() > 0);
    } else {
      setClearIconVisible(false);
      setCanClear(true);
    }
    if (mOnFocusChangeListener != null) {
      mOnFocusChangeListener.onFocusChange(view, hasFocus);
    }
  }
  @Override
  public boolean onTouch(final View view, final MotionEvent motionEvent) {
    final int x = (int) motionEvent.getX();
    if (x > getWidth() - getPaddingRight() - clearTextIcon.getIntrinsicWidth()) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        if (clearTextIcon.isVisible()) {
          setError(null);
          setText("");
        } else if (isCanClear()) {
          setCanClear(false);
          setError(null);
          setText("");
        }
      }
      return true;
    } else {
      return mOnTouchListener != null && mOnTouchListener.onTouch(view,
          motionEvent);
    }
  }
  @Override
  public final void onTextChanged(final CharSequence s, final int start, final
  int before, final int count) {
    if (isFocused()) {
      setClearIconVisible(s.length() > 0);
    }
  }
  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  }
  @Override
  public void afterTextChanged(Editable s) {
  }
  private void setClearIconVisible(final boolean visible) {
    clearTextIcon.setVisible(visible, false);
    final Drawable[] compoundDrawables = getCompoundDrawables();
    setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], visible ?
        clearTextIcon :
        null, compoundDrawables[3]);
  }
  public synchronized boolean isCanClear() {
    return canClear;
  }
  public synchronized void setCanClear(boolean canClear) {
    this.canClear = canClear;
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android常用的數(shù)據(jù)加密方式代碼詳解

    Android常用的數(shù)據(jù)加密方式代碼詳解

    這篇文章主要介紹了Android常用的數(shù)據(jù)加密方式代碼詳解,介紹了四種常見(jiàn)加密算法及代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Android編程使用pull方式解析xml格式文件的方法詳解

    Android編程使用pull方式解析xml格式文件的方法詳解

    這篇文章主要介紹了Android編程使用pull方式解析xml格式文件的方法,結(jié)合實(shí)例形式分析了Android調(diào)用pull解析器操作xml格式文件的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果

    Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法

    Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法

    這篇文章主要給大家介紹了關(guān)于Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android View移動(dòng)的3種方式總結(jié)

    Android View移動(dòng)的3種方式總結(jié)

    這篇文章主要給大家介紹了Android View移動(dòng)的三種方式,在介紹這三種方式之前先介紹了Android坐標(biāo)系的定義規(guī)則以及View的一些位置參數(shù)。有需要的朋友們可以參考借鑒。
    2016-09-09
  • Android MarginDesign控件TabLayout導(dǎo)航欄使用詳解

    Android MarginDesign控件TabLayout導(dǎo)航欄使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android MarginDesign控件TabLayout導(dǎo)航欄使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Toast和Handler的間隔使用實(shí)例

    Toast和Handler的間隔使用實(shí)例

    Toast和Handler的間隔使用實(shí)例,需要的朋友可以參考一下
    2013-05-05
  • Android實(shí)現(xiàn)廣告圖片輪播效果

    Android實(shí)現(xiàn)廣告圖片輪播效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)廣告圖片輪播效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • OkHttp3中默認(rèn)不保持Cookie的解決方法

    OkHttp3中默認(rèn)不保持Cookie的解決方法

    這篇文章主要給大家介紹了關(guān)于OkHttp3中默認(rèn)不保持Cookie的解決方法,文中先對(duì)OKhttp3中的cookies進(jìn)行了簡(jiǎn)單的介紹,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的方法

    Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的方法

    項(xiàng)目需求如果在項(xiàng)目中第一行文字需要添加布局的情況我們應(yīng)該怎么做呢,經(jīng)過(guò)一番考慮和查找我最終選擇了縮進(jìn)的方式解決這個(gè)問(wèn)題,這篇文章主要給大家介紹了關(guān)于Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的相關(guān)資料,需要的朋友可以參考下
    2022-04-04

最新評(píng)論

北京市| 高要市| 互助| 玉树县| 乌苏市| 宁陕县| 南漳县| 聊城市| 福安市| 微博| 密山市| 达日县| 旌德县| 宝坻区| 涟源市| 通渭县| 库尔勒市| 新巴尔虎右旗| 织金县| 葵青区| 卢龙县| 库尔勒市| 平果县| 清镇市| 元阳县| 米脂县| 丰县| 惠州市| 镇江市| 昌邑市| 新民市| 稻城县| 永州市| 朝阳市| 白银市| 丰城市| 宁乡县| 邮箱| 黑龙江省| 吕梁市| 鄂托克前旗|