仿簡書登錄框,可刪除內(nèi)容或顯示密碼框的內(nèi)容

簡書App 是我很喜歡的一款軟件颗品。今天就模仿了一下他的登錄框肯尺。先上圖:


好了下面上代碼,自定義ImgEditText 繼承與EditText躯枢。重寫一些方法则吟。

// lyf on 2016/12/6.
// 自定義的EditText右邊帶圖片,可以設(shè)置點(diǎn)擊事件
public class ImgEditText extends EditText implements TextWatcher {
//控件左邊的圖片
private Drawable leftDrawable = null;
//控件右邊的圖片
private Drawable rightDrawable = null;
// 控件是否有焦點(diǎn)
private boolean hasFoucs;
private IMyRightDrawableClick mightDrawableClick;

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

public ImgEditText(Context context, AttributeSet attrs) {
    //這里構(gòu)造方法也很重要锄蹂,不加這個很多屬性不能再XML里面定義
    this(context, attrs, android.R.attr.editTextStyle);
}

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


//初始化基本圖片
private void init() {
    //獲取RadioButton的圖片集合
    Drawable[] drawables = getCompoundDrawables();

    leftDrawable = drawables[0];
    rightDrawable = drawables[2];

    setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);

    //設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽
    addTextChangedListener(this);
}

//設(shè)置顯示圖片的大小
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
    super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

    //這里只要改后面兩個參數(shù)就好了氓仲,一個寬一個是高,如果想知道為什么可以查找源碼
    if (left != null) {
        left.setBounds(0, 0, 50, 50);
    }
    if (right != null) {
        right.setBounds(0, 0, 50, 50);
    }
    if (top != null) {
        top.setBounds(0, 0, 100, 100);
    }
    if (bottom != null) {
        bottom.setBounds(0, 0, 100, 100);
    }
    setCompoundDrawables(left, top, right, bottom);
}

//光標(biāo)選中時判斷
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    this.hasFoucs = focused;
    if (focused) {
        setImageVisible(getText().length() > 0);
    } else {
        setImageVisible(false);
    }
}

//設(shè)置清除圖標(biāo)的顯示與隱藏得糜,調(diào)用setCompoundDrawables為EditText繪制上去
protected void setImageVisible(boolean flag) {
    //如果當(dāng)前右側(cè)有圖片則覆蓋右側(cè)的圖片敬扛,如果沒有還是顯示原來的圖片
    if (getCompoundDrawables()[2] != null) {
        rightDrawable = getCompoundDrawables()[2];
    }
    if (flag) {
        setCompoundDrawables(getCompoundDrawables()[0], null, rightDrawable, null);
    } else {
        setCompoundDrawables(getCompoundDrawables()[0], null, null, null);
    }
}

//文本框監(jiān)聽事件
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
    if (hasFoucs) {
        if (text.length() > 0) {
            setImageVisible(true);
        } else {
            setImageVisible(false);
        }
    }
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

public void afterTextChanged(Editable s) {

}


/**
 * 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來模擬點(diǎn)擊事件
 * 當(dāng)我們按下的位置 在  EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度  和
 * EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo)朝抖,豎直方向就沒有考慮
 * (參考 http://blog.csdn.net/xiaanming/article/details/11066685/)
 */
@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) {
                //調(diào)用點(diǎn)擊事件(外部實(shí)現(xiàn))
                mightDrawableClick.rightDrawableClick();
            }
        }
    }

    return super.onTouchEvent(event);
}

//設(shè)置右側(cè)按鈕的點(diǎn)擊事件啥箭,外部調(diào)用的時候?qū)崿F(xiàn)該方法
public void setDrawableClick( IMyRightDrawableClick myMightDrawableClick){
    this.mightDrawableClick = myMightDrawableClick;
}

//自定義接口(實(shí)現(xiàn)右邊圖片點(diǎn)擊事件)
public interface IMyRightDrawableClick {
    void rightDrawableClick();
}

//允許外部修改右側(cè)顯示的圖片
public void setRightDrawable(Drawable drawable){
    rightDrawable = drawable;
    setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null,       rightDrawable, null);
}
}

以上就是自定義類的主要代碼了,注釋比較清楚治宣。
布局布局文件里直接引用就好急侥。

<lyf.myimgedittextdemo.ImgEditText
android:id="@+id/pwdIet"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawableLeft="@mipmap/mm_image"
android:drawableRight="@mipmap/eye_normal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp"
android:drawablePadding="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:hint="密碼"
android:inputType="numberPassword" />

下面看代碼中的設(shè)置

    pwdIet = (ImgEditText) this.findViewById(R.id.pwdIet);

    pwdIet.setDrawableClick(new ImgEditText.IMyRightDrawableClick() {
        @Override
        public void rightDrawableClick() {
            if (isHidden) {
                //設(shè)置EditText文本為可見的
                pwdIet.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                pwdIet.setRightDrawable(getResources().getDrawable(R.mipmap.eye_selected));
            } else {
                //設(shè)置EditText文本為隱藏的
                pwdIet.setTransformationMethod(PasswordTransformationMethod.getInstance());
                pwdIet.setRightDrawable(getResources().getDrawable(R.mipmap.eye_normal));
            }
            isHidden = !isHidden;
            pwdIet.postInvalidate();
            //切換后將EditText光標(biāo)置于末尾
            CharSequence charSequence = pwdIet.getText();
            if (charSequence instanceof Spannable) {
                Spannable spanText = (Spannable) charSequence;
                Selection.setSelection(spanText, charSequence.length());
            }

        }
    });

這樣我們的例子就完成了砌滞。
不懂的可以下載看源碼,很簡單坏怪。
源碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末贝润,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子陕悬,更是在濱河造成了極大的恐慌题暖,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捉超,死亡現(xiàn)場離奇詭異胧卤,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)拼岳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進(jìn)店門枝誊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人惜纸,你說我怎么就攤上這事叶撒。” “怎么了耐版?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵祠够,是天一觀的道長。 經(jīng)常有香客問我粪牲,道長古瓤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任腺阳,我火速辦了婚禮落君,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘亭引。我一直安慰自己绎速,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布焙蚓。 她就那樣靜靜地躺著纹冤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪购公。 梳的紋絲不亂的頭發(fā)上赵哲,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機(jī)與錄音君丁,去河邊找鬼枫夺。 笑死,一個胖子當(dāng)著我的面吹牛绘闷,可吹牛的內(nèi)容都是我干的橡庞。 我是一名探鬼主播较坛,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼扒最!你這毒婦竟也來了丑勤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤吧趣,失蹤者是張志新(化名)和其女友劉穎法竞,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體强挫,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡岔霸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了俯渤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片呆细。...
    茶點(diǎn)故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖八匠,靈堂內(nèi)的尸體忽然破棺而出絮爷,到底是詐尸還是另有隱情,我是刑警寧澤梨树,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布坑夯,位于F島的核電站,受9級特大地震影響抡四,放射性物質(zhì)發(fā)生泄漏柜蜈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一床嫌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胸私,春花似錦厌处、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至捷绒,卻和暖如春瑰排,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背暖侨。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工椭住, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人字逗。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓京郑,卻偏偏與公主長得像宅广,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子些举,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容