国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

自定義控件之EditText

2019-11-09 15:47:46
字體:
供稿:網(wǎng)友

1.EditText簡介

      EditText在Android開發(fā)中使用頻率最高的控件之一,它是用戶跟Android應(yīng)用進(jìn)行數(shù)據(jù)傳輸?shù)拇皯簦热鐚?shí)現(xiàn)一個登陸界面,需要用戶輸入賬號密碼,然后我們獲取用戶輸入的內(nèi)容,提交給服務(wù)器進(jìn)行判斷。

2.為什么要自定義EditText控件

     系統(tǒng)自帶的EditText雖然可以實(shí)現(xiàn)一般的EditText功能,但是有的時(shí)候想在賦予EditText一鍵刪除的功能,這樣的話就不需要,一直點(diǎn)刪除鍵了。目前市場上的APP中的EditText基本上都有這個功能了。接下來就開始介紹如何自定義EditText,給自定義的EditText添加2個系統(tǒng)自帶的EditText沒有的功能。第一:一鍵刪除EditText文本內(nèi)容;第二:當(dāng)輸入為空時(shí)添加抖動效果。

3.EditText的一鍵刪除文本效果

如下圖所示,在第二個自定義的EditText中點(diǎn)擊"X"號圖標(biāo)可以一次性清除該EditText中的文本內(nèi)容定義一個光標(biāo)監(jiān)控類,該類實(shí)現(xiàn)了OnFocusChangeListener接口
PRivate class FocusChangeListenerImpl implements OnFocusChangeListener{    @Override    public void onFocusChange(View v, boolean hasFocus)    {        isHasFocus = hasFocus;        if (isHasFocus)        {            boolean isVisible = getText().toString().length() >= 1;            setClearDrawableVisible(isVisible);//當(dāng)光標(biāo)在該控件且文本內(nèi)容不為空時(shí),可刪除圖標(biāo)顯示,可以供用戶刪除文本內(nèi)容        } else        {            setClearDrawableVisible(false);            if (getText().toString().trim().length() == 0)            {                setShakeAnimation();//當(dāng)光標(biāo)離開該控件,且文本內(nèi)容為空時(shí),抖動提示用戶            }        }    }}       再定義一個文字輸入監(jiān)控類,該類實(shí)現(xiàn)了TextWatcher接口,在給類中只實(shí)現(xiàn)了一個方法afterTextChanged用于監(jiān)控文字輸入后的事件,當(dāng)輸入后文本內(nèi)容長度大于等于1則,刪除圖標(biāo)顯示。
//當(dāng)輸入結(jié)束后判斷是否顯示右邊clean的圖標(biāo)private class TextWatcherImpl implements TextWatcher{    @Override    public void afterTextChanged(Editable s)    {        boolean isVisible = getText().toString().length() >= 1;        setClearDrawableVisible(isVisible);    }    @Override    public void beforeTextChanged(CharSequence s, int start, int count, int after)    {    }    @Override    public void onTextChanged(CharSequence s, int start, int before, int count)    {    }}定義一個方法setClearDrawableVisible(boolean isVisible),用于設(shè)置刪除圖標(biāo)是否可見
protected void setClearDrawableVisible(boolean isVisible){    Drawable rightDrawable;    if (isVisible)    {        rightDrawable = mRightDrawable;    } else    {        rightDrawable = null;    }    //使用代碼設(shè)置該控件left, top, right, and bottom處的圖標(biāo)    setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],            rightDrawable, getCompoundDrawables()[3]);}重寫onTouchEvent()方法,當(dāng)單擊范圍在指定范圍時(shí)將觸發(fā)將文本信息清空的操作。
@Overridepublic boolean onTouchEvent(MotionEvent event){    switch (event.getAction())    {        case MotionEvent.ACTION_UP:            boolean isClean = (event.getX() > (getWidth() - getTotalPaddingRight())) &&                    (event.getX() < (getWidth() - getPaddingRight()));            if (isClean)            {                setText("");            }            break;        default:            break;    }    return super.onTouchEvent(event);}初始化控件
private void init(){    Drawable[] drawables = this.getCompoundDrawables();    mRightDrawable = drawables[2];    //設(shè)置焦點(diǎn)變化的監(jiān)聽    this.setOnFocusChangeListener(new FocusChangeListenerImpl());    //設(shè)置EditText文字變化的監(jiān)聽    this.addTextChangedListener(new TextWatcherImpl());    //初始化時(shí)讓右邊clean圖標(biāo)不可見    setClearDrawableVisible(false);}構(gòu)造方法
public CleanableEditText(Context context){    super(context);    init();}public CleanableEditText(Context context, AttributeSet attrs){    super(context, attrs);    init();}public CleanableEditText(Context context, AttributeSet attrs, int defStyle){    super(context, attrs, defStyle);    init();}

4.抖動效果

//CycleTimes動畫重復(fù)的次數(shù)public Animation shakeAnimation(int CycleTimes){    Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);    translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));    translateAnimation.setDuration(1000);    return translateAnimation;}      該代碼的功能是指將EditText在水平方向進(jìn)行抖動抖動范圍為與初始位置相差10dp,抖動CycleTimes次,CycleTimes為所傳參數(shù)。抖動時(shí)間為1秒鐘。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 西青区| 东阿县| 高尔夫| 法库县| 盱眙县| 郧西县| 门头沟区| 会东县| 定南县| 元阳县| 平度市| 汕头市| 措美县| 静乐县| 建昌县| 定安县| 舞阳县| 平南县| 翼城县| 年辖:市辖区| 朝阳区| 如东县| 民乐县| 神农架林区| 雷州市| 虞城县| 南阳市| 宜昌市| 南岸区| 光山县| 宝山区| 米林县| 英吉沙县| 莎车县| 丰宁| 墨竹工卡县| 桐城市| 聂荣县| 尉氏县| 托里县| 库伦旗|