自定义带删除按钮的EditText

在做这个小的控件的时候,其实我知道网上已经有很多了,但是还是想照自己的思路,亲手写下,功能很简单;

就是

   (1)在EditText没有输入内容的时候隐藏删除按钮

(2 )在输入内容的使用显示删除按钮

(3 )在点击删除按钮的时候  ,如果是点击的话删除一个字符,如果是长按的话按照一定的速度一个一个删除,删除每一个字符以后光标必须在最后

看下效果图自定义带删除按钮的EditText自定义带删除按钮的EditText

基本上就这几点,实现起来很简单,我没有通过Extends EditText  来实现,使用的是布局。

        要注意的是 

(1)长按删除的时候要在onLongClick中发送删除的msg,然后开始删除,知道按钮的执行了onkeyUp的时候删除msg,这个是通过Handler来实现的。

(2)imageButton必须要获取到焦点,如果没有焦点点击没有反应

        然后就是代码了:

public class HuahuaDelectEdit extends  LinearLayout implements OnClickListener,OnLongClickListener{
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private EditText mEditText;
	private ImageButton mImageButton;
	private static final long DELECTPRETIME = 500;
	private AttributeSet mAttributeSet;
	private String mTitle;
	private Handler mHandler = new Handler(){
		
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case 1:
				delect();
				mHandler.sendEmptyMessageDelayed(1, DELECTPRETIME);
				break;
				
			default:
				break;
			}
		};
	};
	public HuahuaDelectEdit(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mAttributeSet =attrs;
		this.mContext = context;
		initView();
	}
	
	public HuahuaDelectEdit(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mAttributeSet =attrs;
		this.mContext = context;
		initView();
	}
	
	public HuahuaDelectEdit(Context context) {
		super(context);
		this.mContext = context;
		initView();
	}

	public void initView() {
		TypedArray a = mContext.obtainStyledAttributes(mAttributeSet, R.styleable.delectedit);
		mTitle = a.getString(R.styleable.delectedit_title);
		mLayoutInflater = LayoutInflater.from(mContext);
		View view = mLayoutInflater.inflate(R.layout.item_delect_edit, this);
		mEditText = (EditText)view.findViewById(R.id.et_text);
		mImageButton = (ImageButton)view.findViewById(R.id.ib_delect);
		setHint(mTitle);
		mEditText.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) {
				setDelectShow();
			}
		});
		mImageButton.setOnClickListener(this);
		mImageButton.setOnLongClickListener(this);
		mImageButton.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					Log.e("ACTION_UP", "UP");
					 mHandler.removeMessages(1);;
					break;

				default:
					break;
				}
				return false;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_delect:
//			mEditText.setText("");
			delect();
			break;

		default:
			break;
		}
	}
	
	public void setDelectShow(){
		if (mEditText.getText().toString().length() != 0) {
			mImageButton.setVisibility(View.VISIBLE);
		}else {
			mImageButton.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onLongClick(View v) {
		Log.e("长点击", "点击点击点击");
		mHandler.sendEmptyMessage(1);
		
		return false;
	}
	public void delect(){
		if (mEditText.getText().toString().length() == 0) {
		    mHandler.removeMessages(1);;
			return;
		}
		mEditText.setText(mEditText.getText().subSequence(0, mEditText.getText().toString().length()-1));
		Editable etext = mEditText.getText();
		Selection.setSelection(etext, etext.length());
	}

	public String getText(){
		return mEditText.getText().toString();
	}
	public void setHint(String hint){
		mEditText.setHint(hint);
	}
}



Demo:http://download.csdn.net/detail/u012808234/9084707
上一篇:类型系统-前端发展的里程碑


下一篇:重拾C#教程:高级部分-索引器(Indexer)