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"//隐藏

有误的地方请指正

每日精进

最后神兽镇楼

//┏┓   ┏┓
//┏┛┻━━━┛┻┓
//┃       ┃  
//┃   ━   ┃
//┃ ┳┛ ┗┳ ┃
//┃       ┃
//┃   ┻   ┃
//┃       ┃
//┗━┓   ┏━┛
//  ┃   ┃   神兽保佑        
//  ┃   ┃   代码无BUG!
//  ┃   ┗━━━┓
//  ┃       ┣┓
//  ┃       ┏┛
//  ┗┓┓┏━┳┓┏┛
//    ┃┫┫ ┃┫┫
//    ┗┻┛ ┗┻┛
时间: 2024-08-02 15:13:37

Android 自定义EditText输入框 带清空按钮的相关文章

自定义EditText 实现带清空按钮的输入框

注:本文转载自csdn,其中实现清除功能所采用的方案比较可取. 原文如下: 项目要求:做出包含根据情况可变色的下划线,左侧有可变图标,右侧有可变删除标志的edittext,如图 记录制作过程: 第一版本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar)

/** * 带文本提示的进度条 */ public class TextProgressBar extends ProgressBar { private String text; private Paint mPaint; public TextProgressBar(Context context) { super(context); initText(); } public TextProgressBar(Context context, AttributeSet attrs, int d

我的Android进阶之旅------&gt;Android自定义View实现带数字的进度条(NumberProgressBar)

今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢daimajia的开源奉献! 第一步.效果展示 图1.蓝色的进度条 图2.红色的进度条 图3.多条颜色不同的进度条 图4.多条颜色不同的进度条 版权声明:本文为[欧阳鹏]原创文章,欢迎转载,转载请注明出处! [http://blog.csdn.net/ouyang_peng/article/deta

自定义EditText,带删除按键

带清空按键,还可左右加图片,默认的是加了清空图片. 清空内容是判断的手势点击EditText的范围. 自定义EditText代码如下: import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import androi

Android自定义EditText去除边框并添加下划线

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!--注

Android:自定义DialogFragment的内容和按钮

小问题,记录下~ Android4.0以后开始推荐使用DialogFragment代替Dialog.Android的官方文档中给了两个示例: 一个Basic Dialog 示例了如何自定义窗口内容--重写onCreateView方法. 一个Alert Dialog 示例了如何自定义弹窗的正负按钮--重写onCreateDialog方法. 好的,那么问题来了 在实际应用中经常是需要既自定义窗口内容.又需要自定义按钮的. 这时候如果我们按图索骥,把DialogFragment的onCreateVie

Android--&gt;轻松打造带删除按钮的输入框(EditText),附Emoji表情过滤

输入框带删除按钮, 此乃标配, 实现起来方法也很多, 网上开源也很多. 但是, 没事就喜欢瞎折腾. 上图说话. 只是在原生的基础上加了扩展. 相对来说入侵非常少, 使用方法和原生的一模一样.无任何阉割. 完整代码: public class ExEditText extends AppCompatEditText { Rect clearRect = new Rect(); public ExEditText(Context context) { super(context); } public

给EasyUI的DateBox控件增加一个清空按钮

 EasyUI中的DateBox控件居然没有清空按钮,如下图: 真是不可思议,对于要求日期格式必须选择的情况下,不能清空日期,非常不方便. 虽然可以通过手工修改EasyUI及相关库文件来实现增加清空按钮功能,但这要求修改EasyUI原生库文件,而且要修改多个地方,势必会造成其它的不兼容问题. 但随着1.3.5版本的发布,这个问题可以很好地解决了,而且不需要修改任何库文件,只需要在页面中日期控件中增加一段代码即可.这是因为1.3.5版本的EasyUI增加了一个属性:buttons,这个属性可以

Android 带清除 和 晃动效果的 自定义 EditText 解析

转载请标明出处 :http://blog.csdn.net/qq_19986309 尊重他人劳动成果~ 谢谢 今天给大家实现一个 带清除按钮  和 晃动效果的 自定义 EditText 常常大家写登录 注册按钮 可能是需要用到的, 好了 废话不多说 直接上效果图 这样的效果还是不错的  也是比较实用 不管在哪里的注册登录都用得着 下面贴下代码 public class ClearWriteEditText extends EditText implements View.OnFocusChang