public class EditTextUtils {
private static final String TAG = "EditTextUtils";
public static void setOnFocusChangeListener(Activity activity, final EditText et) {
final Drawable drawable = activity.getResources().getDrawable(R.drawable.del);
//drawable.setBounds(0, 0, 30, 30);//图片位置的微调
drawable.setBounds(-20, 0, 10, 30);//图片位置的微调
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.i(TAG, "获取焦点" + v.getId());
if (et.getText().toString().trim().length() == 0) {
et.setCompoundDrawables(null, null, null, null);//只放左边)
} else {
et.setCompoundDrawables(null, null, drawable, null);//只放左边)
}
} else {
Log.i(TAG, "失去焦点" + v.getId());
et.setCompoundDrawables(null, null, null, null);//只放左边
}
}
});
et.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// et.getCompoundDrawables()得到一个长度为4的数组,分别表示左右上下四张图片
Drawable drawable = et.getCompoundDrawables()[2];
//如果右边没有图片,不再处理
if (drawable == null)
return false;
//如果不是按下事件,不再处理
if (motionEvent.getAction() != MotionEvent.ACTION_UP)
return false;
if (motionEvent.getX() > et.getWidth() - et.getPaddingRight() - drawable.getIntrinsicWidth()) {
et.setText("");
et.setCompoundDrawables(null, null, null, null);
}
return false;
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
Log.i(TAG, "最终长度 = " + editable.length());
if (editable.length() == 0) {
et.setCompoundDrawables(null, null, null, null);
} else {
et.setCompoundDrawables(null, null, drawable, null);
}
}
});
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et1 = (EditText) findViewById(R.id.et1);
EditText et2 = (EditText) findViewById(R.id.et2);
EditText et3 = (EditText) findViewById(R.id.et3);
EditTextUtils.setOnFocusChangeListener(MainActivity.this, et1);
EditTextUtils.setOnFocusChangeListener(MainActivity.this, et2);
EditTextUtils.setOnFocusChangeListener(MainActivity.this, et3);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.ycfy.button.MainActivity">
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>