本篇文章給大家分享的是有關(guān)Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)帶清除功能的輸入框,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
為方城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及方城網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、方城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
一,自定義一個(gè)類,名為ClearEditText
package com.example.clearedittext; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.View.OnFocusChangeListener; import android.view.animation.Animation; import android.view.animation.CycleInterpolator; import android.view.animation.TranslateAnimation; import android.widget.EditText; public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** * 刪除按鈕的引用 */ private Drawable mClearDrawable; /** * 控件是否有焦點(diǎn) */ private boolean hasFoucs; public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { //這里構(gòu)造方法也很重要,不加這個(gè)很多屬性不能再XML里面定義 this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認(rèn)的圖片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { // throw new NullPointerException("You can add drawableRight attribute in XML"); mClearDrawable = getResources().getDrawable(R.drawable.delete_selector); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); //默認(rèn)設(shè)置隱藏圖標(biāo) setClearIconVisible(false); //設(shè)置焦點(diǎn)改變的監(jiān)聽 setOnFocusChangeListener(this); //設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽 addTextChangedListener(this); } /** * 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來模擬點(diǎn)擊事件 * 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和 * EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo),豎直方向就沒有考慮 */ @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) { this.setText(""); } } } return super.onTouchEvent(event); } /** * 當(dāng)ClearEditText焦點(diǎn)發(fā)生變化的時(shí)候,判斷里面字符串長(zhǎng)度設(shè)置清除圖標(biāo)的顯示與隱藏 */ @Override public void onFocusChange(View v, boolean hasFocus) { this.hasFoucs = hasFocus; if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } /** * 設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去 * @param visible */ protected void setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } /** * 當(dāng)輸入框里面內(nèi)容發(fā)生變化的時(shí)候回調(diào)的方法 */ @Override public void onTextChanged(CharSequence s, int start, int count, int after) { if(hasFoucs){ setClearIconVisible(s.length() > 0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } /** * 設(shè)置晃動(dòng)動(dòng)畫 */ public void setShakeAnimation(){ this.setAnimation(shakeAnimation(5)); } /** * 晃動(dòng)動(dòng)畫 * @param counts 1秒鐘晃動(dòng)多少下 * @return */ public static Animation shakeAnimation(int counts){ Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation; } }
里面設(shè)置點(diǎn)擊與輸入的監(jiān)聽的代碼,
setClearIconVisible()方法,設(shè)置隱藏和顯示清除圖標(biāo)的方法,我們這里不是調(diào)用setVisibility()方法,setVisibility()這個(gè)方法是針對(duì)View的,我們可以調(diào)用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)來設(shè)置上下左右的圖標(biāo)
setOnFocusChangeListener(this)為輸入框設(shè)置焦點(diǎn)改變監(jiān)聽,如果輸入框有焦點(diǎn),我們判斷輸入框的值是否為空,為空就隱藏清除圖標(biāo),否則就顯示
addTextChangedListener(this)為輸入框設(shè)置內(nèi)容改變監(jiān)聽,其實(shí)很簡(jiǎn)單呢,當(dāng)輸入框里面的內(nèi)容發(fā)生改變的時(shí)候,我們需要處理顯示和隱藏清除小圖標(biāo),里面的內(nèi)容長(zhǎng)度不為0我們就顯示,否是就隱藏,但這個(gè)需要輸入框有焦點(diǎn)我們才改變顯示或者隱藏,為什么要需要焦點(diǎn),比如我們一個(gè)登陸界面,我們保存了用戶名和密碼,在登陸界面onCreate()的時(shí)候,我們把我們保存的密碼顯示在用戶名輸入框和密碼輸入框里面,輸入框里面內(nèi)容發(fā)生改變,導(dǎo)致用戶名輸入框和密碼輸入框里面的清除小圖標(biāo)都顯示了,這顯然不是我們想要的效果,所以加了一個(gè)是否有焦點(diǎn)的判斷
setShakeAnimation(),這個(gè)方法是輸入框左右抖動(dòng)的方法,之前我在某個(gè)應(yīng)用看到過類似的功能,當(dāng)用戶名錯(cuò)誤,輸入框就在哪里抖動(dòng),感覺挺好玩的,其實(shí)主要是用到一個(gè)移動(dòng)動(dòng)畫,然后設(shè)置動(dòng)畫的變化率為正弦曲線
以上就是Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)帶清除功能的輸入框,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)帶清除功能的輸入框
當(dāng)前地址:http://muchs.cn/article30/ijocpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)