EditText中添加删除按钮

原理 :重写控件而已,直接上代码:

package com.osgsdk.tool;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;

@SuppressLint({ "ViewConstructor" })

/**
 * @author mabinbin
 */

public class EditTextLayout extends EditText
{
	private final static String TAG = "EditTextWithDel";
	private BitmapDrawable imgInable;
	private BitmapDrawable imgAble;
	private Activity activity;
	
	public EditTextLayout(Activity context)
	{
		super(context);
		activity = context;
		init();
	}
	
	public EditTextLayout(Activity context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		activity = context;
		init();
	}
	
	public EditTextLayout(Activity context, AttributeSet attrs)
	{
		super(context, attrs);
		activity = context;
		init();
	}
	
	private void init()
	{
		BitmapDrawable imgAble_h = BitmapAssets.getBitmapAssets(activity, "osg_mini_icon_clean.png");
		imgAble = (BitmapDrawable) BitmapAssets.zoomDrawable ( imgAble_h ,<span style="color:#ff6666;">width  ,height</span>) ;
//		imgAble = imgAble_h ;
		imgInable = BitmapAssets.getBitmapAssets(activity, "");
		addTextChangedListener(new TextWatcher()
		{
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				setDrawable();
			}
		});
		setDrawable();
	}
	
	private void setDrawable()
	{
		if (length() < 1)
		{
			setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
		} else
		{
			setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
		}
	}
	

	@Override
	protected void finalize() throws Throwable
	{
		super.finalize();
	}
	
}
红字标识的width和height全部都是需要自己去设置的,是删除按钮的大小
BitmapAssets.getBitmapAssets
方法是一个自建获取asstes文件夹中的图片的方法,可以自己写,或者之间自己用其他的方法获得都可以,代码我就补贴了,下面要说的是设置按钮大小的方法,也就是
zoomDrawable 
这个方法很实用,在动态设置布局的时候,要求不改变图片尺寸的情况下可以这样做,下面是代码:

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {  
	    int width = drawable.getIntrinsicWidth();  
	    int height = drawable.getIntrinsicHeight();  
	    Bitmap oldbmp = drawableToBitmap(drawable);  
	    Matrix matrix = new Matrix();  
	    float scaleWidth = ((float) w / width);  
	    float scaleHeight = ((float) h / height);  
	    matrix.postScale(scaleWidth, scaleHeight);  
	    Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,  
	            matrix, true);  
	    return new BitmapDrawable(null, newbmp);  
	}  
	
	public static Bitmap drawableToBitmap(Drawable drawable) {  
	    int width = drawable.getIntrinsicWidth();  
	    int height = drawable.getIntrinsicHeight();  
	    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
	            : Bitmap.Config.RGB_565;  
	    Bitmap bitmap = Bitmap.createBitmap(width, height, config);  
	    Canvas canvas = new Canvas(bitmap);  
	    drawable.setBounds(0, 0, width, height);  
	    drawable.draw(canvas);  
	    return bitmap;  
	}  



that's all...


你可能感兴趣的:(EditText中添加删除按钮)