自定義EditText實現一鍵刪除數據


轉載請注明出處http://blog.csdn.net/xiaanming/article/details/11066685

自定義EditText帶刪除小圖標,

實現的功能:

   點擊刪除小圖標,刪除當前輸入框中所有內容

   刪除圖標默認不顯示,當輸入框獲得焦點后顯示,

實現的操作:

在Edittext的DrawableRight中添加一張刪除圖標,作為刪除功能的小圖標

因為Edittext不能為圖片設置點擊監聽事件,因此我們需要自定義Edittext在onTouchEvent方法中模擬按鈕點擊的操作

當我們觸摸抬起(就是ACTION_UP的時候)的范圍  大於輸入框左側到清除圖標左側的距離,小與輸入框左側到清除圖片右側的距離,我們則認為是點擊清除圖片,

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#B0E0E6"
    >

    <TextView
         android:id="@+id/activity_login_ttitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登錄賬號"
        android:textColor="#ffffff"
        android:background="#6699ff"
        android:padding="15dp"
        android:textSize="25sp"
        android:gravity="center"/>
    <ImageView
        android:id="@+id/activity_login_icon"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/activity_login_ttitle"
        android:layout_margin="15dp"/>
  <LinearLayout
      android:id="@+id/activity_login_ll_layout2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center_vertical"
      android:layout_below="@id/activity_login_icon"
      android:padding="15dp"
      >
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:textSize="20sp"
          android:gravity="center_vertical"
          android:textColor="#000000"
          android:text="賬 號:"/>
      <com.my.myapp.customeView.CustomeEditext
          android:id="@+id/activity_login_et_user"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textSize="18sp"
          android:singleLine="true"
          android:background="@drawable/edittext_bg_selector"
          android:drawableRight="@drawable/delete_selector"
          android:hint="請輸入賬號"/>

  </LinearLayout>
    <LinearLayout
        android:id="@+id/activity_login_ll_layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:padding="15dp"
        android:layout_below="@id/activity_login_ll_layout2">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:textColor="#000000"
            android:text="密 碼:"/>
        <com.my.myapp.customeView.CustomeEditext
            android:id="@+id/activity_login_et_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="18sp"
            android:inputType="textPassword"
            android:singleLine="true"
            android:background="@drawable/edittext_bg_selector"
            android:drawableRight="@drawable/delete_selector"
            android:hint="請輸入密碼"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/activity_login_ll_layout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:layout_below="@id/activity_login_ll_layout3">
        <Button
            android:id="@+id/activity_login_btn_login"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="登錄"
            android:textSize="18sp"
            android:padding="15dp"
            android:onClick="onLocalLogin"
            android:background="@drawable/btn__login_style_selector"
            />
        <Button
            android:id="@+id/activity_login_register"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="注冊"
            android:textSize="18sp"
            android:layout_gravity="right"
            android:layout_marginLeft="30dp"
            android:padding="15dp"
            android:onClick="onLocalLogin"
            android:background="@drawable/btn__login_style_selector"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/activity_login_ll_layout5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:padding="10dp"
        >
        <TextView
            android:id="@+id/activity_login_tv_qq"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="QQ"
            android:gravity="center"
            android:drawableTop="@drawable/ssdk_oks_classic_qq"
            android:layout_margin="5dp"
            android:onClick="onOtherLogin"
            android:clickable="true"/>
        <TextView
            android:id="@+id/activity_login_tv_wechat"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="微信"
            android:gravity="center"
            android:drawableTop="@drawable/ssdk_oks_classic_wechat"
            android:layout_margin="5dp"
            android:onClick="onOtherLogin"
            android:clickable="true"/>
        <TextView
            android:id="@+id/activity_login_tv_email"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="微博"
            android:gravity="center"
            android:layout_margin="5dp"
            android:drawableTop="@drawable/ssdk_oks_classic_tencentweibo"
            android:onClick="onOtherLogin"
            android:clickable="true"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_above="@id/activity_login_ll_layout5"
     >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_centerInParent="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="其他賬號登錄"
        android:textColor="#CDCDC1"
        android:padding="7dp"
        android:background="#AFEEEE"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:textSize="12dp"
        />
    </RelativeLayout>
</RelativeLayout>
View Code

 

源代碼:

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; 
    /**
     * 控件是否有焦點
     */
    private boolean hasFoucs;
 
    public ClearEditText(Context context) { 
        this(context, null); 
    } 
 
    public ClearEditText(Context context, AttributeSet attrs) { 
        //這里構造方法也很重要,不加這個很多屬性不能再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,假如沒有設置我們就使用默認的圖片
        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()); 
        //默認設置隱藏圖標
        setClearIconVisible(false); 
        //設置焦點改變的監聽
        setOnFocusChangeListener(this); 
        //設置輸入框里面內容發生改變的監聽
        addTextChangedListener(this); 
    } 
 
 
    /**
  • setClearIconVisible()方法,設置隱藏和顯示清除圖標的方法,我們這里不是調用setVisibility()方法,setVisibility()這個方法是針對View的,我們可以調用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)來設置上下左右的圖標

   setOnFocusChangeListener(this) 為輸入框設置焦點改變監聽,如果輸入框有焦點,我們判斷輸入框的值是否為空,為空就隱藏清除圖標,否則就顯示

  • addTextChangedListener(this) 為輸入框設置內容改變監聽,其實很簡單呢,當輸入框里面的內容發生改變的時候,我們需要處理顯示和隱藏清除小圖標,里面的內容長度不為0我們就顯示,否是就隱藏,但這個需要輸入框有焦點我們才改變顯示或者隱藏,為什么要需要焦點,比如我們一個登陸界面,我們保存了用戶名和密碼,在登陸界面onCreate()的時候,我們把我們保存的密碼顯示在用戶名輸入框和密碼輸入框里面,輸入框里面內容發生改變,導致用戶名輸入框和密碼輸入框里面的清除小圖標都顯示了,這顯然不是我們想要的效果,所以加了一個是否有焦點的判斷
  • setShakeAnimation(),這個方法是輸入框左右抖動的方法,,當用戶名錯誤,輸入框就在哪里抖動,感覺挺好玩的,其實主要是用到一個移動動畫,然后設置動畫的變化率為正弦曲線

效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM