android 带清除功能的EditText
16lz
2021-01-26
先看效果图
当不输入任何内容时候,右边的清除图标不会出现,但是如果只要输入一个字符以后,清除图标就会出现,点击清除图标以后就会清除输入框当中的内容
首先要自定义一个MyEditView
package com.example.edittextcleardemo;import android.content.Context;import android.graphics.drawable.Drawable;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.View.OnFocusChangeListener;import android.widget.EditText;public class MyEditView extends EditText implements TextWatcher,OnFocusChangeListener {/** * 左右两侧图片资源 */private Drawable left, right;/** * 是否获取焦点,默认没有焦点 */private boolean hasFocus = false;/** * 手指抬起时的X坐标 */private int xUp = 0;public MyEditView(Context context) {this(context, null);}public MyEditView(Context context, AttributeSet attrs) {this(context, attrs, android.R.attr.editTextStyle);}public MyEditView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initWedgits(attrs);}/** * 初始化各组件 * @param attrs * 属性集 */private void initWedgits(AttributeSet attrs) {try {left = getCompoundDrawables()[0];right = getCompoundDrawables()[2];initDatas();} catch (Exception e) {e.printStackTrace();}}/** * 初始化数据 */private void initDatas() {try {// 第一次显示,隐藏删除图标setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);addListeners();} catch (Exception e) {e.printStackTrace();}}/** * 添加事件监听 */private void addListeners() {try {setOnFocusChangeListener(this);addTextChangedListener(this);} catch (Exception e) {e.printStackTrace();}}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int after) {if (hasFocus) {if (TextUtils.isEmpty(s)) {// 如果为空,则不显示删除图标setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);} else {// 如果非空,则要显示删除图标if (null == right) {right = getCompoundDrawables()[2];}setCompoundDrawablesWithIntrinsicBounds(left, null, right, null);}}}@Overridepublic boolean onTouchEvent(MotionEvent event) {try {switch (event.getAction()) {case MotionEvent.ACTION_UP:// 获取点击时手指抬起的X坐标xUp = (int) event.getX();// 当点击的坐标到当前输入框右侧的距离小于等于getCompoundPaddingRight()的距离时,则认为是点击了删除图标// getCompoundPaddingRight()的说明:Returns the right padding of the view, plus space for the right Drawable if any.if ((getWidth() - xUp) <= getCompoundPaddingRight()) {if (!TextUtils.isEmpty(getText().toString())) {setText("");}}break;default:break;}} catch (Exception e) {e.printStackTrace();}return super.onTouchEvent(event);}@Overridepublic void afterTextChanged(Editable s) {}@Overridepublic void onFocusChange(View v, boolean hasFocus) {try {this.hasFocus = hasFocus;String msg=getText().toString();if(hasFocus){if(msg.equalsIgnoreCase("")){setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); }else{ setCompoundDrawablesWithIntrinsicBounds(left, null, right, null); }}if(hasFocus==false){setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);}} catch (Exception e) {e.printStackTrace();}}//获取输入内容public String text_String (){return getText().toString();}}
使用的时候只要在需要的布局中引入该控件即可:
android:background="@drawable/edit_input_rectangle" android:drawableRight="@drawable/clear_normal_list" android:hint="请输入用户名" android:textCursorDrawable="@null" android:textSize="14sp" >
这个输入框是圆角的,原因是引用了样式android:background="@drawable/edit_input_rectangle"
这个样式的定义是edit_input_rectangle.xml放在drawable文件当中
<?xml version="1.0" encoding="utf-8"?>
clear_normal_list是清除图标
代码下载地址:http://download.csdn.net/detail/xubeiqiannian/8765683
更多相关文章
- android中短信协议与字段
- android 状态栏(StatusBar)
- Android(安卓)Launcher 如何去掉主菜单,所有应用摆在桌面,类似小米
- drawable( hdpi, ldpi, mdpi, xhdpi)区别
- android 卸载程序、清除数据、停止服务用法
- Android根据日期自动修改Icon
- 解决ADB server didn't ACK * failed to start daemon *
- 菜单栏上没有android机器人的图标(AVD)的解决方法
- flutter APP 应用名称、启动页、图标配置