EditText在Android開(kāi)發(fā)中使用頻率最高的控件之一,它是用戶跟Android應(yīng)用進(jìn)行數(shù)據(jù)傳輸?shù)拇皯?,比如?shí)現(xiàn)一個(gè)登陸界面,需要用戶輸入賬號(hào)密碼,然后我們獲取用戶輸入的內(nèi)容,提交給服務(wù)器進(jìn)行判斷。
定義一個(gè)光標(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)離開(kāi)該控件,且文本內(nèi)容為空時(shí),抖動(dòng)提示用戶 } } }} 再定義一個(gè)文字輸入監(jiān)控類,該類實(shí)現(xiàn)了TextWatcher接口,在給類中只實(shí)現(xiàn)了一個(gè)方法afterTextChanged用于監(jiān)控文字輸入后的事件,當(dāng)輸入后文本內(nèi)容長(zhǎng)度大于等于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) { }}定義一個(gè)方法setClearDrawableVisible(boolean isVisible),用于設(shè)置刪除圖標(biāo)是否可見(jiàn)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)聽(tīng) this.setOnFocusChangeListener(new FocusChangeListenerImpl()); //設(shè)置EditText文字變化的監(jiān)聽(tīng) this.addTextChangedListener(new TextWatcherImpl()); //初始化時(shí)讓右邊clean圖標(biāo)不可見(jiàn) 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.抖動(dòng)效果
//CycleTimes動(dòng)畫重復(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)行抖動(dòng)抖動(dòng)范圍為與初始位置相差10dp,抖動(dòng)CycleTimes次,CycleTimes為所傳參數(shù)。抖動(dòng)時(shí)間為1秒鐘。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注