android自定义view_ClearEditText带清空按钮的输入框
2017-12-19 19:16
399 查看
创建一个类继承EditText import android.annotation.SuppressLint; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import com.example.ztz.android_timer.R; /** * Created by ztz on 2017/12/19. * 自定义view_ClearEditText */ @SuppressLint("AppCompatCustomView") public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher { private Drawable mClearDrawable; private boolean hasFocus; public ClearEditText(Context context) { super(context); } public ClearEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { // getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3) mClearDrawable = ba02 getCompoundDrawables()[2]; // 获取drawableRight if (mClearDrawable == null) { // 如果为空,即没有设置drawableRight,则使用R.drawable.close这张图片 mClearDrawable = getResources().getDrawable(R.drawable.close); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); setOnFocusChangeListener(this); addTextChangedListener(this); // 默认隐藏图标 setDrawableVisible(false); } /** * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件 * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效 */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (getCompoundDrawables()[2] != null) { // 起始位置 int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 结束位置 int end = getWidth(); boolean available = (event.getX() > start) && (event.getX() < end); if (available) { this.setText(""); } } } return super.onTouchEvent(event); } @Override public void onFocusChange(View v, boolean hasFocus) { this.hasFocus = hasFocus; // 当输入文字时,显示图标 if (hasFocus && getText().length() > 0) { setDrawableVisible(true); } else { setDrawableVisible(false); } } @Override public void onTextChanged(CharSequence s, int start, int count, int after) { if (hasFocus) { setDrawableVisible(s.length() > 0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } protected void setDrawableVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } }
相关文章推荐
- Android 自定义EditText输入框 带清空按钮
- Android 自定义EditText输入框 带清空按钮
- Android 自定义EditText输入框带清空按钮
- Android客户端之“微服私访”App的系统学习(七)XRecyclerView快速实现列表界面+自定义Search输入框,软键盘搜索按钮监听+TextView部分样式改变
- Android自定义View带有删除按钮的EditText
- Android自定义View带有删除按钮的EditText
- Android 自定义View--实现带有按钮点击效果的自动补全输入框(搜索框)
- Android控件之带清空按钮(功能)的AutoCompleteTextView自动提示
- Android输入框带删除按钮的自定义View
- Android 自定义View、ViewGroup 实战训练之ClearEditText
- android自定义带清空内容的TextView
- Android自定义View之有动画特效带清除按钮的输入框
- [Android开发]自定义View之TextView局部操作View-SpanTextView
- Android尺子布局和自定义TextView
- android自定义View-垂直滚动的TextView
- android自定义实现textview限制多少行数以后以省略号结尾。 ... 这种省略号不是
- android 自定义组件-带图片的textView
- Android textView字体自定义结合HTml
- Android自定义TextView实现drawableLeft内容居中
- Android自动识别<a>标签的自定义TextView(可自定义点击事件)