EditTextUtils Drawable图片 删除文字

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值