自定義EditText清除功能實(shí)現(xiàn)

效果圖:

GIF.gif

1. 編寫xml布局 代碼 如下:

<com.zjq.cleareditextview.ClearEditTextView
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="5dp"
            android:hint="QQ號(hào)/手機(jī)號(hào)/郵箱號(hào)"
            android:inputType="number"
            android:lines="1"
            android:maxLength="17"
            android:paddingLeft="13dp"
            android:textColorHint="#b9b7b7"
            android:textSize="16sp" />
注意這里的 com.zjq.clearedittextview.ClearEditTextView 要與你的 ClearEditTextView類包名相同

2.創(chuàng)建ClearEditTextView類并繼承EditText

public class ClearEditTextView extends EditText implements View.OnFocusChangeListener,TextWatcher {

// 右邊的刪除按鈕
private Drawable mClearDrawable;

public ClearEditTextView(Context context) {
    this(context, null);
}

public ClearEditTextView(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.editTextStyle);
}

public ClearEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    //獲取右邊刪除按鈕
    mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
    //設(shè)置刪除按鈕的邊界
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());

    //設(shè)置默認(rèn)隱藏刪除按鈕
    setClearIcon(false);

    //監(jiān)聽(tīng)EditText焦點(diǎn)變化 ,根據(jù)text長(zhǎng)度控制刪除按鈕的顯示 .隱藏
    setOnFocusChangeListener(this);
    //監(jiān)聽(tīng)文本內(nèi)容變化
    addTextChangedListener(this);
}

/**
 * 控制EditText右邊制刪除按鈕的顯示、隱藏
 */
/**
 * 控制EditText右邊制刪除按鈕的顯示、隱藏
 */
private void setClearIcon(boolean isShow) {
    Drawable rightDrawable = isShow ? mClearDrawable : null;
    setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],
            rightDrawable, getCompoundDrawables()[3]);
}  }
注意這里的R.drawable.delete_selector。

3.在miamap下面放入兩張刪除圖片

一張是默認(rèn)顯示的圖片

命名為:search_clear_normal.png

一張是按下刪除按鈕顯示的圖片

命名為:search_clear_pressed

4.在drawable文件下創(chuàng)建delete_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
< selector xmlns:android="http://schemas.android.com/apk/res/android">

<!--按下?tīng)顟B(tài)-->
<item android:state_pressed="true" android:drawable="@mipmap/search_clear_pressed" />

<!--默認(rèn)顯示-->
<item android:drawable="@mipmap/search_clear_normal" />

</selector>

設(shè)置引用mipmap下面的圖片。
到此已經(jīng)可以實(shí)現(xiàn)清除功能了!!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,267評(píng)論 25 708
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,893評(píng)論 2 45
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,375評(píng)論 0 17
  • 我們先來(lái)看一個(gè)屌絲逆襲的美發(fā)故事 某某 亞洲短發(fā)小王子 也許有許多人不知道他 兩年前他和絕大多數(shù)發(fā)型師一樣,窮逼,...
    一號(hào)美發(fā)師閱讀 3,423評(píng)論 8 21
  • 今年研一的學(xué)生 從小就被父母訓(xùn)練的很獨(dú)立 可以自己?jiǎn)为?dú)完成的事情從來(lái)不尋求他人的幫助 這也就讓我和父母之間沒(méi)有太多...
    舊時(shí)光999閱讀 197評(píng)論 0 1

友情鏈接更多精彩內(nèi)容