Android自定義控件EditText使用詳解
本文實例為大家分享了Android自定義控件EditText的具體代碼,供大家參考,具體內容如下
自定義控件分三種:
1. 自繪控件
2. 組合控件
3. 繼承控件
代碼已上傳到 github
以后的自定義控件就都放這個倉庫
需求
這里由于項目的需要實現(xiàn)一個自定義EditText,主要實現(xiàn)的為兩點,一個是工具圖標toolIcon,例如點擊清除EditText內容。一個為EditText左邊的提示圖標hintIcon, 例如輸入賬號密碼時前面的圖標。
為了讓這個控件的拓展性更高,設置了兩個點擊事件接口。對于toolIcon來說,默認點擊事件為清除EditText內容,如果需要更改,在代碼中設設置相關的點擊事件即可。
步驟
繼承EditText
編寫attrs.xml, 創(chuàng)建declare-styleable
編寫MyEditText
布局中使用
實現(xiàn)
獲取布局文件中設置的屬性
這里返回的是一個TypedArray數(shù)組,獲取之后就可以獲得布局文件中設置的屬性了
private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.getTheme().obtainStyledAttributes( attrs, R.styleable.MyEditText, 0, 0); hintIcon = typedArray.getDrawable(R.styleable.MyEditText_hintIcon); toolIcon = typedArray.getDrawable(R.styleable.MyEditText_toolIcon); fixed = typedArray.getBoolean(R.styleable.MyEditText_fixed, true); if (toolIcon != null && fixed) { setHeight(toolIcon.getIntrinsicHeight()); } setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); setCompoundDrawablePadding(10); typedArray.recycle(); onClickListenerWithEditTextToolIcon = new OnClickListenerWithEditTextToolIcon() { @Override public void onClick() { setText(""); } }; }
設置資源圖片
EditText是繼承自TextView,在TextView中存在兩個方法
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom) setCompoundDrawables(left, top, right, bottom)
是設置資源圖片的位置,第一個方法和第二個方法的區(qū)別在于第一個方法中資源圖片的大小是由系統(tǒng)來獲取圖片固有的大小,第二個方法則是需要自己通過LayoutParams設置大小。
設置點擊事件
我們通過setCompoundDrawables()等方法設置的圖片,而由于在父類中并沒有提供相關的圖片點擊處理接口,因此可以重寫onTouchEvent()來實現(xiàn)相關的點擊事件,只需要根據(jù)我們手指落點或抬起點的位置就可以判斷手指是否點擊了相關圖片。在這里,我選擇了手指抬起時處理
/** * Override the touchEvent to judge whether click toolIcon or hintIcon * * @param event motionEvent * @return super */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (hintIcon != null) { if (event.getX() < hintIcon.getIntrinsicWidth() && event.getX() > 0) { if (getCompoundDrawables()[0] != null && onClickListenerWithEditTextHintIcon != null) { onClickListenerWithEditTextHintIcon.onClick(); } } } if (toolIcon != null) { if (event.getX() > (getWidth() - toolIcon.getIntrinsicWidth()) && event.getX() < getWidth()) { if (getCompoundDrawables()[2] != null ) { onClickListenerWithEditTextToolIcon.onClick(); } } } } return super.onTouchEvent(event); } /** * interface when click hintIcon */ public interface OnClickListenerWithEditTextHintIcon { void onClick(); } /** * interface when click toolIcon */ public interface OnClickListenerWithEditTextToolIcon { void onClick(); }
完整代碼
package com.customwidget.lzqwidget.cuswidget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.EditText; import com.customwidget.lzqwidget.R; /** * Custom widget of EditText with two icon. * Created by lizhongquan on 16-1-6. */ public class MyEditText extends EditText { private Drawable hintIcon = null; private Drawable toolIcon = null; /** * HintIcon clickListener */ private OnClickListenerWithEditTextHintIcon onClickListenerWithEditTextHintIcon = null; /** * Default clear the EditText */ private OnClickListenerWithEditTextToolIcon onClickListenerWithEditTextToolIcon = null; /** * Default fixed the Height */ private boolean fixed = true; public MyEditText(Context context) { super(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.getTheme().obtainStyledAttributes( attrs, R.styleable.MyEditText, 0, 0); hintIcon = typedArray.getDrawable(R.styleable.MyEditText_hintIcon); toolIcon = typedArray.getDrawable(R.styleable.MyEditText_toolIcon); fixed = typedArray.getBoolean(R.styleable.MyEditText_fixed, true); if (toolIcon != null && fixed) { setHeight(toolIcon.getIntrinsicHeight()); } setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); setCompoundDrawablePadding(10); typedArray.recycle(); onClickListenerWithEditTextToolIcon = new OnClickListenerWithEditTextToolIcon() { @Override public void onClick() { setText(""); } }; } /** * Override the touchEvent to judge whether click toolIcon or hintIcon * * @param event motionEvent * @return super */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (hintIcon != null) { if (event.getX() < hintIcon.getIntrinsicWidth() && event.getX() > 0) { if (getCompoundDrawables()[0] != null && onClickListenerWithEditTextHintIcon != null) { onClickListenerWithEditTextHintIcon.onClick(); } } } if (toolIcon != null) { if (event.getX() > (getWidth() - toolIcon.getIntrinsicWidth()) && event.getX() < getWidth()) { if (getCompoundDrawables()[2] != null ) { onClickListenerWithEditTextToolIcon.onClick(); } } } } return super.onTouchEvent(event); } /** * the clickListener of click hintIcon * * @param clickListenerOfHintIcon OnClickListenerWithEditTextHintIcon */ public void setOnClickListenerWithEditTextHintIcon( OnClickListenerWithEditTextHintIcon clickListenerOfHintIcon) { this.onClickListenerWithEditTextHintIcon = clickListenerOfHintIcon; } /** * the clickListener of click toolIcon * * @param clickListenerOfToolIcon OnClickListenerWithEditTextToolIcon */ public void setOnClickListenerWithEditTextToolIcon( OnClickListenerWithEditTextToolIcon clickListenerOfToolIcon) { this.onClickListenerWithEditTextToolIcon = clickListenerOfToolIcon; } /** * onTextChange * * @param text text * @param start start * @param lengthBefore lengthBefore * @param lengthAfter lengthAfter */ @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); if (text.length() > 0 && getCompoundDrawables()[2] == null && toolIcon != null) { // hintIcon.setBounds(10, 0, 10, 0); setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, toolIcon, null); } if (text.length() == 0 && getCompoundDrawables()[2] != null && toolIcon != null) { setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); } } /** * interface when click hintIcon */ public interface OnClickListenerWithEditTextHintIcon { void onClick(); } /** * interface when click toolIcon */ public interface OnClickListenerWithEditTextToolIcon { void onClick(); } }
attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyEditText"> <attr name="hintIcon" format="integer" /> <attr name="toolIcon" format="integer" /> <attr name="fixed" format="boolean" /> </declare-styleable> </resources>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- android自定義控件ImageView實現(xiàn)圓形圖片
- Android自定義控件ImageView實現(xiàn)點擊之后出現(xiàn)陰影效果
- Android自定義控件ViewFipper實現(xiàn)豎直跑馬燈效果
- Android自定義控件打造絢麗平行空間引導頁
- Android自定義控件EditText實現(xiàn)清除和抖動功能
- Android自定義控件實現(xiàn)下拉刷新效果
- 基于Android自定義控件實現(xiàn)雷達效果
- Android編程實現(xiàn)自定義控件的方法示例
- Android自定義控件之日期選擇控件使用詳解
- Android自定義控件實現(xiàn)九宮格解鎖功能
- 實例講解Android自定義控件
相關文章
Android動態(tài)更新Menu菜單的實現(xiàn)過程
菜單是用戶界面中最常見的元素之一,使用非常頻繁,下面這篇文章主要給大家介紹了關于Android動態(tài)更新Menu菜單的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09Android框架Volley使用:ImageRequest請求實現(xiàn)圖片加載
這篇文章主要介紹了Android框架Volley使用:ImageRequest請求實現(xiàn)圖片加載的相關知識,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05Android開發(fā)必知 九種對話框的實現(xiàn)方法
App中少不了與用戶交互的各種dialog,以此達到很好的用戶體驗,下面給大家介紹Android開發(fā)必知 九種對話框的實現(xiàn)方法,有需要的朋友可以參考下2015-08-08Android UI體驗之全屏沉浸式透明狀態(tài)欄樣式
這篇文章主要介紹了Android UI體驗之全屏沉浸式透明狀態(tài)欄效果的相關資料,需要的朋友可以參考下2017-01-01Android實現(xiàn)類似qq微信消息懸浮窗通知功能
這篇文章主要介紹了Android實現(xiàn)類似qq微信消息懸浮窗通知,需要的朋友可以參考下2018-02-02Android編程實現(xiàn)讀取工程中的txt文件功能
這篇文章主要介紹了Android編程實現(xiàn)讀取工程中的txt文件功能,結合實例形式詳細分析了Android讀取txt文件的原理、操作步驟與相關實現(xiàn)技巧,需要的朋友可以參考下2017-02-02