Android Edittext Drawable 点击卡住焦点

Posted

技术标签:

【中文标题】Android Edittext Drawable 点击卡住焦点【英文标题】:Android Edittext Drawable click stuck focus 【发布时间】:2017-12-23 10:12:27 【问题描述】:

我在一个布局上有三个编辑文本。当它为空时,我删除正确的可绘制对象。当它不为空时,我添加了可绘制对象。当您单击可绘制时,我会清除编辑文本。

当我点击 rightdrawable 时,光标卡住了,下划线保持颜色。我很少有 2-3 光标。为什么??我做错了什么?

不好:

好的:

布局:

  <EditText
        android:id="@+id/et1"
        android:layout_
        android:layout_
        android:drawableRight="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/et2"
        android:layout_
        android:layout_
        android:drawableRight="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/et3"
        android:layout_
        android:layout_
        android:drawableRight="@mipmap/ic_launcher"/>

MainActivity 公共类 MainActivity 扩展 AppCompatActivity

private DrawableState etstate1;
private DrawableState etstate2;
private DrawableState etstate3;
private EditText et2;
private EditText et1;
private EditText et3;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et1 = (EditText)findViewById(R.id.et1);
    et2 = (EditText)findViewById(R.id.et2);
    et3 = (EditText)findViewById(R.id.et3);


    etstate1 = new DrawableState(et1);
    etstate2 = new DrawableState(et2);
    etstate3 = new DrawableState(et3);

    et1.setOnTouchListener(new View.OnTouchListener() 
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) 

            if (DrawableClick.isDrawableClick(motionEvent, et1, DRAWABLE_RIGHT)) 
                et1.setText("");
                etstate1.refreshDrawable();
                return true;
            
            return false;
        
    );

    et2.setOnTouchListener(new View.OnTouchListener() 
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) 

            if (DrawableClick.isDrawableClick(motionEvent, et2, DRAWABLE_RIGHT)) 
                et2.setText("");
                etstate2.refreshDrawable();
                return true;
            
            return false;
        
    );

    et3.setOnTouchListener(new View.OnTouchListener() 
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) 

            if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) 
                et3.setText("");
                etstate3.refreshDrawable();
                return true;
            
            return false;
        
    );


    et1.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) 
            etstate1.refreshDrawable();
        
    );

    et2.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) 
            etstate2.refreshDrawable();
        
    );

    et3.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) 
            etstate3.refreshDrawable();
        


     );

    

助手类:

public class DrawableClick 


    public static boolean isDrawableClick(MotionEvent event, EditText editText, DrawablePositions drawablePosition) 
        if (event.getAction() == MotionEvent.ACTION_UP) 
            if (editText.getCompoundDrawables()[drawablePosition.position] != null && event.getX() >= (editText.getRight() - editText.getLeft() - editText.getCompoundDrawables()[drawablePosition.position].getBounds().width())) 
                return true;
            
        
        return false;
    


    public enum DrawablePositions 
        DRAWABLE_LEFT(0),
        DRAWABLE_TOP(1),
        DRAWABLE_RIGHT(2),
        DRAWABLE_BOTTOM(3);

        private int position;

        DrawablePositions(int pos) 
            this.position = pos;
        
        
    

下一步:

公共类 DrawableState

private EditText editText;
private Drawable[] editTextDrawables;

public DrawableState(EditText editText) 
    this.editText = editText;
    saveDrawableState();
    refreshDrawable();



public void saveDrawableState() 
    this.editTextDrawables = editText.getCompoundDrawables();


public void refreshDrawable() 

    if (editText.getText().toString().equals("")) 
        editText.setCompoundDrawables(null, null, null, null);
        editText.clearFocus();
     else 
        if (editTextDrawables != null) 
            editText.setCompoundDrawables(editTextDrawables[0], editTextDrawables[1], editTextDrawables[2], editTextDrawables[3]);
        
    


【问题讨论】:

【参考方案1】:

我找到了解决办法。

改变它

       if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) 
               ...
                return true;
            
            return false;

收件人:

  if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) 
            ...
            motionEvent.setAction(MotionEvent.ACTION_CANCEL);
        
        return false;

【讨论】:

以上是关于Android Edittext Drawable 点击卡住焦点的主要内容,如果未能解决你的问题,请参考以下文章

解决android手机EditText设置光标颜色,android:textCursorDrawable="@drawable/corner_cursor" 华为手机无效果的问题

在edittext中移动drawable的位置

为 EditText 的 Drawable 权限设置 onClickListener [重复]

删除EditText中的其他下划线

Android:设置EditText 背景

关于EditText一些效果