Android自定義控件EditText使用詳解
本文實(shí)例為大家分享了Android自定義控件EditText的具體代碼,供大家參考,具體內(nèi)容如下
自定義控件分三種:
1. 自繪控件
2. 組合控件
3. 繼承控件
代碼已上傳到 github
以后的自定義控件就都放這個倉庫
需求
這里由于項(xiàng)目的需要實(shí)現(xiàn)一個自定義EditText,主要實(shí)現(xiàn)的為兩點(diǎn),一個是工具圖標(biāo)toolIcon,例如點(diǎn)擊清除EditText內(nèi)容。一個為EditText左邊的提示圖標(biāo)hintIcon, 例如輸入賬號密碼時前面的圖標(biāo)。
為了讓這個控件的拓展性更高,設(shè)置了兩個點(diǎn)擊事件接口。對于toolIcon來說,默認(rèn)點(diǎn)擊事件為清除EditText內(nèi)容,如果需要更改,在代碼中設(shè)設(shè)置相關(guān)的點(diǎn)擊事件即可。
步驟
繼承EditText
編寫attrs.xml, 創(chuàng)建declare-styleable
編寫MyEditText
布局中使用
實(shí)現(xiàn)
獲取布局文件中設(shè)置的屬性
這里返回的是一個TypedArray數(shù)組,獲取之后就可以獲得布局文件中設(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("");
}
};
}
設(shè)置資源圖片
EditText是繼承自TextView,在TextView中存在兩個方法
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom) setCompoundDrawables(left, top, right, bottom)
是設(shè)置資源圖片的位置,第一個方法和第二個方法的區(qū)別在于第一個方法中資源圖片的大小是由系統(tǒng)來獲取圖片固有的大小,第二個方法則是需要自己通過LayoutParams設(shè)置大小。
設(shè)置點(diǎn)擊事件
我們通過setCompoundDrawables()等方法設(shè)置的圖片,而由于在父類中并沒有提供相關(guān)的圖片點(diǎn)擊處理接口,因此可以重寫onTouchEvent()來實(shí)現(xiàn)相關(guān)的點(diǎn)擊事件,只需要根據(jù)我們手指落點(diǎn)或抬起點(diǎn)的位置就可以判斷手指是否點(diǎn)擊了相關(guān)圖片。在這里,我選擇了手指抬起時處理
/**
* 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義控件ImageView實(shí)現(xiàn)圓形圖片
- Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后出現(xiàn)陰影效果
- Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果
- Android自定義控件打造絢麗平行空間引導(dǎo)頁
- Android自定義控件EditText實(shí)現(xiàn)清除和抖動功能
- Android自定義控件實(shí)現(xiàn)下拉刷新效果
- 基于Android自定義控件實(shí)現(xiàn)雷達(dá)效果
- Android編程實(shí)現(xiàn)自定義控件的方法示例
- Android自定義控件之日期選擇控件使用詳解
- Android自定義控件實(shí)現(xiàn)九宮格解鎖功能
- 實(shí)例講解Android自定義控件
相關(guān)文章
Activity實(shí)例詳解之啟動activity并返回結(jié)果
這篇文章主要介紹了Activity實(shí)例詳解之啟動activity并返回結(jié)果 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android中Serializable和Parcelable序列化對象詳解
這篇文章主要介紹了Android中Serializable和Parcelable序列化對象的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-02-02
Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結(jié)合實(shí)例形式分析了AudioManager調(diào)節(jié)音量的常用方法及相關(guān)使用技巧,需要的朋友可以參考下2016-08-08
Android Metro菜單實(shí)現(xiàn)思路及代碼
在安卓平臺上實(shí)現(xiàn)一下Metro菜單效果,之前有介紹過了,相信大家對此不會陌生了吧,感興趣的朋友可以了解下哈2013-06-06
Android實(shí)現(xiàn)短信驗(yàn)證碼自動填寫
這篇文章主要為大家詳細(xì)介紹了Android短信驗(yàn)證碼自動填寫功能的實(shí)現(xiàn)過程,感興趣的小伙伴們可以參考一下2016-08-08

