在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實(shí)現(xiàn)一鍵清空輸入框
百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點(diǎn)擊button跳轉(zhuǎn)到撥號界面實(shí)例
百戰(zhàn)經(jīng)典第十一戰(zhàn)-GridView動(dòng)態(tài)添加Item
百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件
百戰(zhàn)經(jīng)典第五戰(zhàn)-各種對話框Dialog精彩薈萃
百戰(zhàn)經(jīng)典第八戰(zhàn)-BitmapFactory.Options對資源圖片進(jìn)行縮放
百戰(zhàn)經(jīng)典第四戰(zhàn)-玩轉(zhuǎn)ListView
百戰(zhàn)經(jīng)典第十五-竊聽風(fēng)云之短信監(jiān)聽
前言
百戰(zhàn)經(jīng)典第十四戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas用戶表查詢功能實(shí)現(xiàn)
百戰(zhàn)經(jīng)典第九戰(zhàn)-ViewFlipper實(shí)現(xiàn)幻燈效果
百戰(zhàn)經(jīng)典第三戰(zhàn)-實(shí)現(xiàn)畫圖板
百戰(zhàn)經(jīng)典第十七戰(zhàn)-基于加速度傳感器的搖一搖功能實(shí)例
百戰(zhàn)經(jīng)典第十戰(zhàn)-LayoutAnimation布局動(dòng)畫效果
百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計(jì)時(shí)的Button按鈕
百戰(zhàn)經(jīng)典第六戰(zhàn)-Activity啟動(dòng)模式小樣
百戰(zhàn)經(jīng)典第十二戰(zhàn)-GridView動(dòng)態(tài)刪除Item
百戰(zhàn)經(jīng)典第十六戰(zhàn)-圖片或頭像設(shè)置功能
百戰(zhàn)經(jīng)典第十九戰(zhàn)-短信監(jiān)聽實(shí)現(xiàn)驗(yàn)證碼自動(dòng)填入
百戰(zhàn)經(jīng)典第一戰(zhàn)—聽話的TextView
百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實(shí)現(xiàn)一鍵清空輸入框
百戰(zhàn)經(jīng)典第十三戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas實(shí)現(xiàn)用戶注冊功能

百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實(shí)現(xiàn)一鍵清空輸入框

自定義View實(shí)現(xiàn)登錄注冊頁面的EditText一鍵清空功能,效果如下:

這里寫圖片描述

輸入框輸入文字后自動(dòng)出現(xiàn)一鍵清空鍵,輸入框文字為空時(shí),一鍵清空鍵隱藏,下面我們看一下如何通過自定義View實(shí)現(xiàn)這一效果。

看一下DeletableEditText.java:

package com.example.testview;
//省略導(dǎo)入包
/**
 * @author yayun email:291214603@qq.com blog: http://blog.csdn.net/yayun0516
 * @date 2015-08-11 TODO
 */
public class DeletableEditText extends EditText {
    private Drawable mRightDrawable;
    private boolean isHasFocus;
    public DeletableEditText(Context context) {
        super(context);
        init();
    }
    public DeletableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public DeletableEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    private void init() {
        Drawable[] drawables = this.getCompoundDrawables();

        // 取得right位置的Drawable
        // 即我們在布局文件中設(shè)置的android:drawableRight
        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);
    }
    /**
     * 當(dāng)手指抬起的位置在clean的圖標(biāo)的區(qū)域 我們將此視為進(jìn)行清除操作 getWidth():得到控件的寬度
     * event.getX():抬起時(shí)的坐標(biāo)(改坐標(biāo)是相對于控件本身而言)
     * getTotalPaddingRight():clean的圖標(biāo)左邊緣至控件右邊緣的距離
     * getPaddingRight():clean的圖標(biāo)右邊緣至控件右邊緣的距離 于是: getWidth() -
     * getTotalPaddingRight()表示: 控件左邊到clean的圖標(biāo)左邊緣的區(qū)域 getWidth() -
     * getPaddingRight()表示: 控件左邊到clean的圖標(biāo)右邊緣的區(qū)域
     * 
     */
    @Override
    public 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 class FocusChangeListenerImpl implements OnFocusChangeListener {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            isHasFocus = hasFocus;
            if (isHasFocus) {
                boolean isVisible = getText().toString().length() >= 1;
                setClearDrawableVisible(isVisible);
            } else {
                setClearDrawableVisible(false);
            }
        }
    }
    // 當(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) {
        }
    }
    // 隱藏或顯示右邊clean的圖標(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]);
    }
    // 顯示動(dòng)畫
    public void setShakeAnimation() {
        this.startAnimation(shakeAnimation(5));

    }
    // CycleTimes動(dòng)畫重復(fù)的次數(shù)
    public Animation shakeAnimation(int CycleTimes) {
        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 10);
        translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
        translateAnimation.setDuration(1000);
        return translateAnimation;
    }
}

DeletableEditText 繼承自EditText,設(shè)置了焦點(diǎn)監(jiān)聽setOnFocusChangeListener和輸入框文字變化監(jiān)聽addTextChangedListener,初始的時(shí)候通過setClearDrawableVisible方法設(shè)置最右邊的“x”圖片不可見。覆寫了onTouchEvent方法,覆寫了onFocusChange方法,判斷在獲得焦點(diǎn)且輸入長度不為零的時(shí)候?qū)⒆钣疫叺摹皒”圖片顯示。本部分還通過shakeAnimation方法實(shí)現(xiàn)了動(dòng)畫效果,可以在Activity中調(diào)用此方法,用于判空操作。 布局文件代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="登  錄"
        android:textSize="25sp" />
    <com.example.testview.DeletableEditText
        android:id="@+id/det_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:drawableLeft="@drawable/user_account"
        android:drawableRight="@drawable/user_delete"
        android:ems="10"
        android:hint="請輸入帳號名" />
    <com.example.testview.DeletableEditText
        android:id="@+id/user_password_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/det_test"
        android:layout_marginTop="10dp"
        android:drawableLeft="@drawable/user_password"
        android:drawableRight="@drawable/user_delete"
        android:ems="10"
        android:hint="請輸入密碼"
        android:inputType="textPassword"
        android:singleLine="true" />
</RelativeLayout>

引用自定義控件時(shí),要輸入包.類的全路徑,即:com.example.testview.DeletableEditText。采用了相對布局的方式,并且第一個(gè)DeletableEditText控件設(shè)置了hint的屬性,該屬性可以作為輸入框提示。第二個(gè)輸入框設(shè)置了inputType為textPassword,以密文的方式顯示。