本文实现的效果:

文本框输入为空时显示输入的图标;不为空时显示清空的图标,此时点击清空图标能清空文本框内输入文字。

实现效果:

核心代码:

packagecom.example.test;importandroid.app.Activity;importandroid.content.res.Resources;importandroid.graphics.drawable.Drawable;importandroid.os.Bundle;importandroid.text.Editable;importandroid.text.InputType;importandroid.text.TextUtils;importandroid.text.TextWatcher;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.View.OnTouchListener;importandroid.widget.EditText;publicclassSearchActivityextendsActivity{privateDrawablemIconSearchDefault;//搜索文本框默认图标privateDrawablemIconSearchClear;//搜索文本框清除文本内容图标privateEditTextmSearchView;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);finalResourcesres=getResources();//搜索图标mIconSearchDefault=res.getDrawable(R.drawable.txt_search_default);//清除图标mIconSearchClear=res.getDrawable(R.drawable.txt_search_clear);mSearchView=(EditText)findViewById(R.id.edt_search);mSearchView.addTextChangedListener(tbxSearch_TextChanged);mSearchView.setOnTouchListener(txtSearch_OnTouch);//设置默认图标mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,null,mIconSearchDefault,null);}/***动态搜索*/privateTextWatchertbxSearch_TextChanged=newTextWatcher(){//缓存上一次文本框内是否为空privatebooleanisnull=true;@OverridepublicvoidafterTextChanged(Editables){if(TextUtils.isEmpty(s)){if(!isnull){mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,null,mIconSearchDefault,null);isnull=true;}}else{if(isnull){mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,null,mIconSearchClear,null);isnull=false;}}}@OverridepublicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){}/***随着文本框内容改变动态改变列表内容*/@OverridepublicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){//查询搜索}};/**点击清除图标,清空文本框*/privateOnTouchListenertxtSearch_OnTouch=newOnTouchListener(){@OverridepublicbooleanonTouch(Viewv,MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_UP:intcurX=(int)event.getX();//判断触摸位置在右端,并且文本框内有内容if(curX>v.getWidth()-88&&!TextUtils.isEmpty(mSearchView.getText())){mSearchView.setText("");intcacheInputType=mSearchView.getInputType();//backuptheinputtypemSearchView.setInputType(InputType.TYPE_NULL);//disablesoftinputmSearchView.onTouchEvent(event);//callnativehandlermSearchView.setInputType(cacheInputType);//restoreinputtypereturntrue;//consumetoucheven}break;}returnfalse;}};}

    代码说明:

      1. 为输入框绑定触摸事件(模拟点击事件捕捉)。通过监听点击区域判断是否点击清空图片,

如果在该区域并且文本框不为空,则清空文本框。

      2. 为输入框绑定文本改变事件监听,根据内容改变动态设置图标显示。

      3. 维持清空操作后软键盘状态。


更多相关文章

  1. Android(安卓)| 1. Fragment中使用SQLite实现简单存储及查询
  2. android 开发-Toast控件的实现
  3. eclipse编译cocos2d-x2.2.6的Android
  4. Android(安卓)TextView显示html富文本格式以及Edittext获取带htm
  5. [UE4.4.x] 虚幻4 安卓打包
  6. Android-------Activity之间通信
  7. Android(安卓)程式开发:(九)使用活动栏 —— 9.3自定义ActionBar项
  8. ListView的OnItemClickListener事件被其内部Button点击事件屏蔽
  9. android studio编写运行java main的三种方法(亲测)

随机推荐

  1. android选择和裁剪图像拍摄的图像
  2. Android Animation 动画解析
  3. Android 多媒体应用——MediaPlayer与Sur
  4. Android 如何有效修改包名
  5. android studio 编译打包包含jni的sdk需
  6. Android(安卓)HIDL lshal
  7. Android(安卓)Gallery组件详解
  8. Android 屏幕刷新机制:ViewRootImpl、Chor
  9. 解决onConfigurationChanged不被调用
  10. Android DataBinding & MVVM