Android-EditText(自定义带删除功能的EditText)
来源:互联网 发布:linux 自建邮局 编辑:程序博客网 时间:2024/04/28 18:48
1.说明
自定义带删除功能的EditText有两种方法,第一种是用组合视图的方法,即在一个view视图里面左侧放置一个EditText,右侧放置一个ImageView,但是这样增加了视图的层次,而且对输入内容的长度要做一定的处理。
第二种是重新定义EditText组件,增加相应的事件处理,即可达到很好的效果,效果图如下:
2.ClearEditText的JAVA类文件
-
-
-
-
- public class ClearEditText extends EditText implements OnFocusChangeListener,
- TextWatcher {
-
- private Drawable mClearDrawable;
- private boolean hasFoucs;
-
- public ClearEditText(Context context) {
- this(context, null);
- }
-
- public ClearEditText(Context context, AttributeSet attrs) {
- this(context, attrs, android.R.attr.editTextStyle);
- }
-
- public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
-
- private void init() {
-
- mClearDrawable = getCompoundDrawables()[2];
- if (mClearDrawable == null) {
- mClearDrawable = getResources().getDrawable(
- R.drawable.edit_delete);
- }
-
- mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(),
- mClearDrawable.getIntrinsicHeight());
-
- setClearIconVisible(false);
-
- setOnFocusChangeListener(this);
-
- addTextChangedListener(this);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_UP) {
- if (getCompoundDrawables()[2] != null) {
- int x = (int)event.getX();
- int y = (int)event.getY();
- Rect rect = getCompoundDrawables()[2].getBounds();
- int height = rect.height();
- int distance = (getHeight() - height)/2;
- boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
- boolean isInnerHeight = y > distance && y <(distance + height);
- if (isInnerWidth && isInnerHeight) {
- this.setText("");
- }
- }
- }
- return super.onTouchEvent(event);
- }
-
-
-
-
-
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- this.hasFoucs = hasFocus;
- if (hasFocus) {
- setClearIconVisible(getText().length() > 0);
- } else {
- setClearIconVisible(false);
- }
- }
-
- protected void setClearIconVisible(boolean visible) {
- Drawable right = visible ? mClearDrawable : null;
- setCompoundDrawables(getCompoundDrawables()[0],
- getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
- }
-
- @Override
- public void onTextChanged(CharSequence s, int start, int count, int after) {
- if (hasFoucs) {
- setClearIconVisible(s.length() > 0);
- }
- }
-
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
-
- }
-
- @Override
- public void afterTextChanged(Editable s) {
-
- }
-
-
- }
3.引用ClearEditText的XML文件
- <com.once.android_ui.selfview.ClearEditText
- android:id="@+id/user_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:drawableLeft="@drawable/user_name"
- android:drawablePadding="7dp"
- android:hint="@string/name_tip"
- android:singleLine="true"
- android:textSize="17sp" >
- <requestFocus />
- </com.once.android_ui.selfview.ClearEditText>
附件是图片资源文件。
1 0