国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義控件EditText使用詳解

2019-10-22 18:21:03
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晴隆县| 云霄县| 彭水| 柞水县| 和平县| 公安县| 乌海市| 育儿| 大田县| 巴彦淖尔市| 广汉市| 武定县| 高碑店市| 淮滨县| 孝感市| 德化县| 姜堰市| 齐齐哈尔市| 卓资县| 丹巴县| 东乡| 莱州市| 阿巴嘎旗| 库伦旗| 克什克腾旗| 富源县| 成安县| 宝清县| 斗六市| 瓦房店市| 宜阳县| 湖州市| 博野县| 奎屯市| 英德市| 乳源| 东台市| 广安市| 静宁县| 曲沃县| 梨树县|