android自定义文本框,android自定义文本框,后面带清空按钮

android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示

package com.qc.health.weight;

import com.qc.health.R;

import android.annotation.SuppressLint;

import

android.content.Context;

import android.graphics.Rect;

import

android.graphics.drawable.Drawable;

import android.text.Editable;

import

android.text.TextWatcher;

import android.util.AttributeSet;

import

android.view.MotionEvent;

import android.widget.EditText;

public class MyEditText extends EditText {

private final String TAG =

"MyEditText";

private Drawable dRight;

private Rect rBounds;

// 构造器

public MyEditText(Context paramContext) {

super(paramContext);

initEditText();

}

public MyEditText(Context paramContext, AttributeSet paramAttributeSet)

{

super(paramContext, paramAttributeSet);

initEditText();

}

public MyEditText(Context paramContext, AttributeSet

paramAttributeSet,

int paramInt) {

super(paramContext, paramAttributeSet, paramInt);

initEditText();

}

// 初始化edittext 控件

@SuppressLint("ResourceAsColor")

private void

initEditText() {

setEditTextDrawable();

this.setBackgroundResource(R.drawable.edittext_bg);

this.setTextColor(R.color.login_txt);

this.setHintTextColor(R.color.login_remind_txt);

this.setPadding(20,

3, 6, 3);

this.setTextSize(14);

this.setSingleLine(true);

addTextChangedListener(new TextWatcher() {

// 对文本内容改变进行监听

public void afterTextChanged(Editable paramEditable)

{

}

public void beforeTextChanged(CharSequence

paramCharSequence,

int paramInt1, int paramInt2, int

paramInt3) {

}

public void onTextChanged(CharSequence paramCharSequence,

int paramInt1, int paramInt2, int paramInt3)

{

MyEditText.this.setEditTextDrawable();

}

});

}

// 控制图片的显示

private void setEditTextDrawable() {

if

(getText().toString().length() == 0){

setCompoundDrawables(null,

null, null, null);

} else {

setCompoundDrawables(null, null, this.dRight, null);

}

}

//失去焦点事件

@Override

protected void onFocusChanged(boolean

focused, int direction,

Rect previouslyFocusedRect) {

//获得焦点,判断是否有内容

if (this.hasFocus() == true) {

//如果没有内容,则保护显示清空按钮,否则就显示

if (getText().toString().length() == 0){

setCompoundDrawables(null, null, null, null);

} else {

setCompoundDrawables(null, null, this.dRight, null);

}

}else{

//失去焦点,隐藏按钮

setCompoundDrawables(null, null, null,

null);

}

super.onFocusChanged(focused, direction,

previouslyFocusedRect);

}

protected void finalize() throws Throwable {

super.finalize();

this.dRight = null;

this.rBounds =

null;

}

// 添加触摸事件

@SuppressWarnings("unused")

public boolean

onTouchEvent(MotionEvent paramMotionEvent) {

if ((this.dRight !=

null) && (paramMotionEvent.getAction() == 1) &&

getText().toString().length() > 0) {

this.rBounds =

this.dRight.getBounds();

int i = (int)

paramMotionEvent.getX();

int ix = (int)

paramMotionEvent.getRawX();

// int e = (int)this.getX();

//

int ex = (int)this.getRotationX();

int width =

this.getWidth();

int left = (int)this.getLeft();

int right = (int)this.getRight();

// int marginWidth = right -

this.rBounds.width()-10;

int marginWidth = width -

this.rBounds.width()-5;

// int marginWidth = i - (getRight() - 3 *

(this.rBounds.width()+10));

// if (marginWidth > -40 &&

marginWidth < 40) {

if (marginWidth < i && i <=

width) {

setText("");

paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);

}

}

return super.onTouchEvent(paramMotionEvent);

}

// 设置显示的图片资源

public void setCompoundDrawables(Drawable

paramDrawable1,

Drawable paramDrawable2, Drawable

paramDrawable3,

Drawable paramDrawable4) {

if

(paramDrawable3 != null)

this.dRight = paramDrawable3;

super.setCompoundDrawables(paramDrawable1, paramDrawable2,

paramDrawable3, paramDrawable4);

}

}

注意使用的时候是这样的

android:id="@id/login_etUserNameId"

android:layout_width="200dp"

android:layout_height="35dp"

android:layout_gravity="center"

android:layout_marginLeft="5dp"

android:drawableRight="@drawable/del"

android:hint="@string/login_et_account_txt" />

android:drawableRight="@drawable/del"这个属性是必带的,是清空按钮的图片

原文:http://www.cnblogs.com/qc-huihui/p/3513222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值