Android 自定义EditText输入框带清空按钮

 更新时间:2017年05月18日 11:00:17   投稿:lqh  
这篇文章主要介绍了Android 自定义EditText输入框带清空按钮的相关资料,需要的朋友可以参考下

Android 自定义EditText输入框带清空按钮

当用户输入字符后 EditText会自动在输入框的内部右侧出现删除按钮

重写EditText达到简化布局的效果

效果图:

继承EditText

package com.example.myedittexttest;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

public class MyEditText extends EditText {
	private final String TAG = "MyEditText";
	private Drawable dRight;
	private Rect rBounds;

	public MyEditText(Context paramContext) {
		super(paramContext);
		initEditText();
	}

	public MyEditText(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		initEditText();
	}

	public MyEditText(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
		initEditText();
	}

	// 初始化edittext 控件
	private void initEditText() {
		setEditTextDrawable();
		addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听
			@Override
			public void afterTextChanged(Editable paramEditable) {
			}

			@Override
			public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
			}

			@Override
			public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
				MyEditText.this.setEditTextDrawable();
			}
		});
	}

	// 控制图片的显示
	public void setEditTextDrawable() {
		if (getText().toString().length() == 0) {
			setCompoundDrawables(null, null, null, null);
		} else {
			setCompoundDrawables(null, null, this.dRight, null);
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		this.dRight = null;
		this.rBounds = null;

	}

	/**
	 * 添加触摸事件 点击之后 出现 清空editText的效果
	 */
	@Override
	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		if ((this.dRight != null) && (paramMotionEvent.getAction() == 1)) {
			this.rBounds = this.dRight.getBounds();
			int i = (int) paramMotionEvent.getRawX();// 距离屏幕的距离
			// int i = (int) paramMotionEvent.getX();//距离边框的距离
			if (i > getRight() - 3 * this.rBounds.width()) {
				setText("");
				paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
			}
		}
		return super.onTouchEvent(paramMotionEvent);
	}

	/**
	 * 显示右侧X图片的
	 * 
	 * 左上右下
	 */
	@Override
	public void setCompoundDrawables(Drawable paramDrawable1, Drawable paramDrawable2, Drawable paramDrawable3, Drawable paramDrawable4) {
		if (paramDrawable3 != null)
			this.dRight = paramDrawable3;
		super.setCompoundDrawables(paramDrawable1, paramDrawable2, paramDrawable3, paramDrawable4);
	}
}

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >

  <com.example.myedittexttest.MyEditText
    android:id="@+id/edit_text"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginTop="50dp"
    android:background="#88aaff"
    android:drawableRight="@drawable/edit_clear"
    android:textCursorDrawable="@null" />

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/edit_text"
    android:layout_marginTop="84dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Button" />

</RelativeLayout>

XML中的属性简介:

显示右侧的X 按钮:

android:drawableRight="@drawable/edit_clear"

设置光标的颜色 设置@null 表示光标的颜色和输入框的字体颜色相同

android:textCursorDrawable="@null"

显示隐藏光标

android:cursorVisible="true"//显示

android:cursorVisible="false"//隐藏

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Android 曲线图的绘制示例代码

    Android 曲线图的绘制示例代码

    本篇文章主要介绍了Android 曲线图的绘制示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android仿新浪微博个人信息界面及其他效果

    Android仿新浪微博个人信息界面及其他效果

    这篇文章主要为大家详细介绍了Android仿新浪微博个人信息界面及其他效果设计,如正则表达式如何匹配相应表情字段,处理微博发出时间距现在时刻的时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • 去除arraylist容器中的相同的对象元素的方法

    去除arraylist容器中的相同的对象元素的方法

    下面小编就为大家带来一篇去除arraylist容器中的相同的对象元素的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Android内置的OkHttp用法介绍

    Android内置的OkHttp用法介绍

    okhttp是一个第三方类库,用于android中请求网络。这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient
    2022-08-08
  • Android自定义View 实现水波纹动画引导效果

    Android自定义View 实现水波纹动画引导效果

    在android程序开发中,我们经常简单通过自定义view实现水波纹动画引导功能,下面通过本文给大家分享实现代码,需要的朋友参考下
    2017-01-01
  • Android实现在ServiceManager中加入自定义服务的方法详解

    Android实现在ServiceManager中加入自定义服务的方法详解

    这篇文章主要介绍了Android实现在ServiceManager中加入自定义服务的方法,结合实例形式分析了Android开发中ServiceManager自定义服务的相关创建与使用方法,需要的朋友可以参考下
    2017-08-08
  • Android开发入门之Notification用法分析

    Android开发入门之Notification用法分析

    这篇文章主要介绍了Android中Notification用法,较为详细的分析了Notification的功能、使用步骤与相关注意事项,需要的朋友可以参考下
    2016-07-07
  • Android App数据格式Json解析方法和常见问题

    Android App数据格式Json解析方法和常见问题

    JSON数据格式,在Android中被广泛运用于客户端和网络(或者说服务器)通信,非常有必要系统的了解学习。恰逢本人最近对json做了一个简单的学习,特此总结一下,以飨各位
    2014-03-03
  • Android ListView弹性效果的实现方法

    Android ListView弹性效果的实现方法

    这篇文章主要为大家详细介绍了Android ListView弹性效果的实现方法,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android中dumpsys命令用法简单介绍

    Android中dumpsys命令用法简单介绍

    这篇文章主要介绍了Android中dumpsys命令用法简单介绍的相关资料,需要的朋友可以参考下
    2017-03-03

最新评论