类:

packageview.design.jaesoon.editclearmodule;

importandroid.graphics.drawable.Drawable;

importandroid.text.Editable;

importandroid.text.TextWatcher;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.widget.EditText;

/**

* Created by Jsesoon on 2016/5/10.

*/

public classEditClearListenerimplementsView.OnTouchListener,View.OnFocusChangeListener,TextWatcher {

privateEditTexteditText;

publicEditClearListener(EditText editText) {

this.editText= editText;

editText.setOnTouchListener(this);

editText.setOnFocusChangeListener(this);

editText.addTextChangedListener(this);

}

@Override

public voidonFocusChange(View v, booleanhasFocus) {

if(editText.getText().toString().length() >0) {

if(hasFocus) {

Drawable drawable =null;

if(android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP) {

drawable = v.getContext().getResources().getDrawable(R.mipmap.ic_launcher,v.getContext().getTheme());

}else{

drawable = v.getContext().getResources().getDrawable(R.mipmap.ic_launcher);

}

// 这一步必须要做,否则不会显示.

drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());

editText.setCompoundDrawables(null, null,drawable, null);

}else{

editText.setCompoundDrawables(null, null, null, null);

}

}

}

@Override

public booleanonTouch(View v,MotionEvent event) {

switch(event.getAction()) {

caseMotionEvent.ACTION_UP:

EditText editText = (EditText) v;

booleanisClean = (event.getX() > (editText.getWidth() - editText.getTotalPaddingRight())) &&

(event.getX() < (editText.getWidth() - editText.getPaddingRight()));

if(isClean) {

editText.setText("");

}

break;

}

return false;

}

@Override

public voidbeforeTextChanged(CharSequence s, intstart, intcount, intafter) {

}

@Override

public voidonTextChanged(CharSequence s, intstart, intbefore, intcount) {

if(editText.getText().toString().length() >0) {

Drawable drawable =null;

if(android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP) {

drawable =editText.getContext().getResources().getDrawable(R.mipmap.ic_launcher,editText.getContext().getTheme());

}else{

drawable =editText.getContext().getResources().getDrawable(R.mipmap.ic_launcher);

}

// 这一步必须要做,否则不会显示.

drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());

editText.setCompoundDrawables(null, null,drawable, null);

}else{

editText.setCompoundDrawables(null, null, null, null);

}

}

@Override

public voidafterTextChanged(Editable s) {

}

}



使用方法:

EditText et = new EditText;

new EditClearListener(et);

更多相关文章

  1. android 多行文本显示的textView
  2. 调用摄像头拍照并显示
  3. Android数组显示
  4. checkBox判定
  5. Android组件 文本框(EditText)
  6. Android(安卓)ViewPager动画切换
  7. 网络请求工具类HttpUtils
  8. Android(安卓)ListView使用方法
  9. 弹出NumberPicker窗口,修改字体大小

随机推荐

  1. Android(安卓)资源文件中@、@android:typ
  2. android中系统自带主题集锦
  3. Android字母索引、Android通讯录索引、An
  4. Android(安卓)保活
  5. Android乱码
  6. Android(安卓)开发实践(一)开始你的 Hello
  7. Android常用布局
  8. Android中的android:layout_width和andro
  9. Android(安卓)SDK 下载地址
  10. 安卓-android:layout_width和android:wid