广告
返回顶部
首页 > 资讯 > 精选 >Android 自定义EditText输入框带清空按钮
  • 577
分享到

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

androidedittext清空按钮 2023-05-31 03:05:23 577人浏览 安东尼
摘要

Android 自定义EditText输入框带清空按钮当用户输入字符后 EditText会自动在输入框的内部右侧出现删除按钮重写EditText达到简化布局的效果效果图:继承EditTextpackage com.example.myedi

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

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

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

效果图:

Android 自定义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() { // 对文本内容改变进行监听@Overridepublic void afterTextChanged(Editable paramEditable) {}@Overridepublic void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {}@Overridepublic 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);}}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();this.dRight = null;this.rBounds = null;}@Overridepublic 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);}@Overridepublic void setCompoundDrawables(Drawable paramDrawable1, Drawable paramDrawable2, Drawable paramDrawable3, Drawable paramDrawable4) {if (paramDrawable3 != null)this.dRight = paramDrawable3;super.setCompoundDrawables(paramDrawable1, paramDrawable2, paramDrawable3, paramDrawable4);}}

--结束END--

本文标题: Android 自定义EditText输入框带清空按钮

本文链接: https://www.lsjlt.com/news/223320.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作