Android自定義控件EditText使用詳解
本文實(shí)例為大家分享了Android自定義控件EditText的具體代碼,供大家參考,具體內(nèi)容如下
自定義控件分三種:
1. 自繪控件
2. 組合控件
3. 繼承控件
代碼已上傳到 github
以后的自定義控件就都放這個(gè)倉(cāng)庫(kù)
需求
這里由于項(xiàng)目的需要實(shí)現(xiàn)一個(gè)自定義EditText,主要實(shí)現(xiàn)的為兩點(diǎn),一個(gè)是工具圖標(biāo)toolIcon,例如點(diǎn)擊清除EditText內(nèi)容。一個(gè)為EditText左邊的提示圖標(biāo)hintIcon, 例如輸入賬號(hào)密碼時(shí)前面的圖標(biāo)。
為了讓這個(gè)控件的拓展性更高,設(shè)置了兩個(gè)點(diǎn)擊事件接口。對(duì)于toolIcon來(lái)說(shuō),默認(rèn)點(diǎn)擊事件為清除EditText內(nèi)容,如果需要更改,在代碼中設(shè)設(shè)置相關(guān)的點(diǎn)擊事件即可。
步驟
繼承EditText
編寫(xiě)attrs.xml, 創(chuàng)建declare-styleable
編寫(xiě)MyEditText
布局中使用
實(shí)現(xiàn)
獲取布局文件中設(shè)置的屬性
這里返回的是一個(gè)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中存在兩個(gè)方法
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom) setCompoundDrawables(left, top, right, bottom)
是設(shè)置資源圖片的位置,第一個(gè)方法和第二個(gè)方法的區(qū)別在于第一個(gè)方法中資源圖片的大小是由系統(tǒng)來(lái)獲取圖片固有的大小,第二個(gè)方法則是需要自己通過(guò)LayoutParams設(shè)置大小。
設(shè)置點(diǎn)擊事件
我們通過(guò)setCompoundDrawables()等方法設(shè)置的圖片,而由于在父類(lèi)中并沒(méi)有提供相關(guān)的圖片點(diǎn)擊處理接口,因此可以重寫(xiě)onTouchEvent()來(lái)實(shí)現(xiàn)相關(guān)的點(diǎn)擊事件,只需要根據(jù)我們手指落點(diǎn)或抬起點(diǎn)的位置就可以判斷手指是否點(diǎn)擊了相關(guān)圖片。在這里,我選擇了手指抬起時(shí)處理
/**
* 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義控件ImageView實(shí)現(xiàn)圓形圖片
- Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后出現(xiàn)陰影效果
- Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果
- Android自定義控件打造絢麗平行空間引導(dǎo)頁(yè)
- Android自定義控件EditText實(shí)現(xiàn)清除和抖動(dòng)功能
- Android自定義控件實(shí)現(xiàn)下拉刷新效果
- 基于Android自定義控件實(shí)現(xiàn)雷達(dá)效果
- Android編程實(shí)現(xiàn)自定義控件的方法示例
- Android自定義控件之日期選擇控件使用詳解
- Android自定義控件實(shí)現(xiàn)九宮格解鎖功能
- 實(shí)例講解Android自定義控件
相關(guān)文章
Android9.0 靜默安裝源碼的實(shí)現(xiàn)
這篇文章主要介紹了Android9.0 靜默安裝源碼的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Android動(dòng)態(tài)更新Menu菜單的實(shí)現(xiàn)過(guò)程
菜單是用戶界面中最常見(jiàn)的元素之一,使用非常頻繁,下面這篇文章主要給大家介紹了關(guān)于Android動(dòng)態(tài)更新Menu菜單的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Android框架Volley使用:ImageRequest請(qǐng)求實(shí)現(xiàn)圖片加載
這篇文章主要介紹了Android框架Volley使用:ImageRequest請(qǐng)求實(shí)現(xiàn)圖片加載的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
Android開(kāi)發(fā)必知 九種對(duì)話框的實(shí)現(xiàn)方法
App中少不了與用戶交互的各種dialog,以此達(dá)到很好的用戶體驗(yàn),下面給大家介紹Android開(kāi)發(fā)必知 九種對(duì)話框的實(shí)現(xiàn)方法,有需要的朋友可以參考下2015-08-08
Android UI體驗(yàn)之全屏沉浸式透明狀態(tài)欄樣式
這篇文章主要介紹了Android UI體驗(yàn)之全屏沉浸式透明狀態(tài)欄效果的相關(guān)資料,需要的朋友可以參考下2017-01-01
android 處理配置變更的實(shí)現(xiàn)方法
這篇文章主要介紹了android 處理配置變更的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Android實(shí)現(xiàn)類(lèi)似qq微信消息懸浮窗通知功能
這篇文章主要介紹了Android實(shí)現(xiàn)類(lèi)似qq微信消息懸浮窗通知,需要的朋友可以參考下2018-02-02
Android獲取熱點(diǎn)主機(jī)ip和連接熱點(diǎn)手機(jī)ip的代碼
這篇文章主要介紹了Android獲取熱點(diǎn)主機(jī)ip和連接熱點(diǎn)手機(jī)ip的相關(guān)資料,需要的朋友可以參考下2018-01-01
Android編程實(shí)現(xiàn)讀取工程中的txt文件功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)讀取工程中的txt文件功能,結(jié)合實(shí)例形式詳細(xì)分析了Android讀取txt文件的原理、操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02

