记录
代码动态设置
有文本的时候显示删除按钮 没有文本的时候 隐藏删除按钮
et_searching.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
if(p0.toString().length>1){
et_searching.setCompoundDrawablesWithIntrinsicBounds(resources.getDrawable(R.mipmap.common_search),null,resources.getDrawable(R.mipmap.icon_clear_et),null)
}else {
et_searching.setCompoundDrawablesWithIntrinsicBounds(resources.getDrawable(R.mipmap.common_search),null,null,null)
}
}
override fun afterTextChanged(p0: Editable?) {
}
})
删除按钮的监听
et_searching.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
var drawable: Drawable? = et_searching.compoundDrawables[2]
?: return false // et.getCompoundDrawables()得到一个长度为4的数组,分别表示左右上下四张图片
//如果右边没有图片,不再处理
//如果不是按下事件,不再处理
if (event!!.action != MotionEvent.ACTION_UP) return false
if (event.x > (et_searching.width
- et_searching.paddingRight
- drawable?.intrinsicWidth!!)
) {
et_searching.setText("")
}
return false
}
})
网友评论