本文實例為大家分享了Android自定義控件EditText的具體代碼,供大家參考,具體內容如下
自定義控件分三種:
1. 自繪控件
2. 組合控件
3. 繼承控件
代碼已上傳到 github
以后的自定義控件就都放這個倉庫
需求
這里由于項目的需要實現一個自定義EditText,主要實現的為兩點,一個是工具圖標toolIcon,例如點擊清除EditText內容。一個為EditText左邊的提示圖標hintIcon, 例如輸入賬號密碼時前面的圖標。
為了讓這個控件的拓展性更高,設置了兩個點擊事件接口。對于toolIcon來說,默認點擊事件為清除EditText內容,如果需要更改,在代碼中設設置相關的點擊事件即可。
步驟
繼承EditText
編寫attrs.xml, 創建declare-styleable
編寫MyEditText
布局中使用
實現
獲取布局文件中設置的屬性
這里返回的是一個TypedArray數組,獲取之后就可以獲得布局文件中設置的屬性了
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)
是設置資源圖片的位置,第一個方法和第二個方法的區別在于第一個方法中資源圖片的大小是由系統來獲取圖片固有的大小,第二個方法則是需要自己通過LayoutParams設置大小。
設置點擊事件
我們通過setCompoundDrawables()等方法設置的圖片,而由于在父類中并沒有提供相關的圖片點擊處理接口,因此可以重寫onTouchEvent()來實現相關的點擊事件,只需要根據我們手指落點或抬起點的位置就可以判斷手指是否點擊了相關圖片。在這里,我選擇了手指抬起時處理
/** * 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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答