安卓自訂帶刪除表徵圖的輸入框EditView,安卓editview

來源:互聯網
上載者:User

安卓自訂帶刪除表徵圖的輸入框EditView,安卓editview

在安卓中我們使用預設的Editview是只能輸入文字的,但是想要刪除,我們得利用IME的刪除按鈕來一個個刪除,現在在好多應用當中,會在輸入框的最後出現一個刪除圖片,點擊就清空了所有的資料,這個很方便。下面我們來實現一下。

先看下:



我們這裡實現的是,當輸入框有文本是,才會出現這個刪除表徵圖。當輸入為空白是,就會消失,其實就是自訂一個Ediiview:

 /** * 輸入文字框 右邊有內建的刪除按鈕 當有輸入時,顯示刪除按鈕,當無輸入時,不顯示刪除按鈕。 *  *  */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.button_login_delete);}mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());// 預設設定隱藏表徵圖setClearIconVisible(false);// 設定焦點改變的監聽setOnFocusChangeListener(this);// 設定輸入框裡面內容發生改變的監聽addTextChangedListener(this);}/** * 因為我們不能直接給EditText設定點擊事件,所以我們用記住我們按下的位置來類比點擊事件 當我們按下的位置 在 EditText的寬度 - * 表徵圖到控制項右邊的間距 - 表徵圖的寬度 和 EditText的寬度 - 表徵圖到控制項右邊的間距之間我們就算點擊了表徵圖,豎直方向就沒有考慮 */@Overridepublic 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);}/** * 當ClearEditText焦點發生變化的時候,判斷裡面字串長度設定清除表徵圖的顯示與隱藏 */@Overridepublic void onFocusChange(View v, boolean hasFocus) {this.hasFoucs = hasFocus;if (hasFocus) {setClearIconVisible(getText().length() > 0);} else {setClearIconVisible(false);}}/** * 設定清除表徵圖的顯示與隱藏,調用setCompoundDrawables為EditText繪製上去 *  * @param visible */protected void setClearIconVisible(boolean visible) {Drawable right = visible ? mClearDrawable : null;setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);}/** * 當輸入框裡面內容發生變化的時候回調的方法 */@Overridepublic void onTextChanged(CharSequence s, int start, int count, int after) {if (hasFoucs) {setClearIconVisible(s.length() > 0);}}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void afterTextChanged(Editable s) {}}

我們在使用的時候,只要替換Editview,換成我們自訂的就行了:

<com.test.view.ClearEditText                    android:id="@+id/edit_user"                    android:layout_width="1px"                    android:layout_height="fill_parent"                    android:layout_marginLeft="10.0dip"                    android:layout_marginRight="15.0dip"                    android:layout_weight="3"                    android:background="@android:color/white"                    android:hint="帳號/郵箱/手機號碼"                    android:inputType="textPersonName"                    android:maxLength="30"                    android:paddingLeft="10.0dip"                    android:singleLine="true"                    android:textSize="16.0sp" />


這樣就會有我們想要的效果了。完整demo我就不提供了。






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.