EditText在Android開發(fā)中使用頻率最高的控件之一,它是用戶跟Android應(yīng)用進(jìn)行數(shù)據(jù)傳輸?shù)拇皯簦热鐚?shí)現(xiàn)一個登陸界面,需要用戶輸入賬號密碼,然后我們獲取用戶輸入的內(nèi)容,提交給服務(wù)器進(jìn)行判斷。
定義一個光標(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秒鐘。
新聞熱點(diǎn)
疑難解答