1.写一个自定义的控件

public class ClearEditText extends AppCompatEditText implements 
        View.OnFocusChangeListener, TextWatcher { 
 
    public Drawable mClearDrawable; 
    private boolean hasFocus; 
 
    private static final String TAG = "ClearEditText"; 
     
    public ClearEditText(Context context, AttributeSet attrs) { 
        super(context, attrs); 
 
        init(); 
    } 
    public void init(){ 
        //右侧清除按钮的图片资源 
        mClearDrawable = getCompoundDrawables()[2]; 
 
        if(mClearDrawable==null){ 
            mClearDrawable = getResources().getDrawable(R.drawable.i_delete); 
        } 
 
        mClearDrawable.setBounds(0,0,mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); 
 
        //设置清除按钮隐藏 
        setClearIconVisble(false); 
        //设置焦点改变的监听 
        setOnFocusChangeListener(this); 
        //设置输入框里面内容发生改变的监听 
        addTextChangedListener(this); 
    } 
    //设置焦点改变的监听 
 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
 
        if(event.getAction()== MotionEvent.ACTION_UP){ 
            if(getCompoundDrawables()[2]!=null){ 
                Boolean touchable = event.getX()>(getWidth()-getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight()))); 
                if(touchable){ 
                    this.setText(""); 
                } 
            } 
        } 
        return super.onTouchEvent(event); 
    } 
 
 
 
    /* 
    *  设置清除按钮的样式 
    * */ 
    private void setClearIconVisble(Boolean visible){ 
        Drawable right =visible ?mClearDrawable :null; 
        setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]); 
    } 
 
    /* 
    * 判断是否显示清除标签的方法 
    * */ 
    private void showClearIcon(){ 
    
        if(hasFocus && getText().length()>0){ 
 
            setClearIconVisble(true); 
        }else{ 
            setClearIconVisble(false); 
        } 
    } 
 
 
    @Override 
    public void onFocusChange(View view, boolean b) { 
 
        hasFocus=b; 
        showClearIcon(); 
    } 
 
    @Override 
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
 
    } 
 
    @Override 
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
        showClearIcon(); 
    } 
 
    @Override 
    public void afterTextChanged(Editable editable) { 
 
    } 
}

2.在布局文件中使用它

 <com.lingdangmao.demo_zidingyi_textview.ClearEditText 
        android:id="@+id/clearedit" 
        android:background="@drawable/rounded_edittext" 
        android:drawablePadding="8dp" 
        android:singleLine="true" 
        android:drawableLeft="@drawable/search" 
        android:drawableRight="@drawable/i_delete" 
        android:hint="请输入要搜索的商品" 
        android:imeOptions="actionSearch" 
        android:layout_width="match_parent" 
        android:layout_height="45dp" />

3.在activity里面写点击回车的执行逻辑

public class MainActivity extends AppCompatActivity implements View.OnKeyListener { 
 
    private ClearEditText clearEditText; 
    private String txt; 
    private static final String TAG = "MainActivity"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        clearEditText =findViewById(R.id.clearedit); 
        clearEditText.setOnKeyListener(this); 
    } 
 
    @Override 
    public boolean onKey(View view, int i, KeyEvent keyEvent) { 
        //这里要做下keyEvent.getAction()==KeyEvent.ACTION_UP,防止执行2次一样的操作 
        if(i==keyEvent.KEYCODE_ENTER && keyEvent.getAction()==KeyEvent.ACTION_UP){ 
            txt=clearEditText.getText().toString().trim(); 
            if(!TextUtils.isEmpty(txt)){ 
                //点击后的具体逻辑写在这里 
                Log.d(TAG, "onKey: "+txt); 
            } 
        } 
        return false; 
    } 
}

4.完成

有个问题,不知道为什么不能设置drawright的图片,只能用默认的删除图片

发布评论
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

获得edittext的图片大小讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。