Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Edittext可绘制点击锁定焦点_Java_Android_Android Layout - Fatal编程技术网

Java Android Edittext可绘制点击锁定焦点

Java Android Edittext可绘制点击锁定焦点,java,android,android-layout,Java,Android,Android Layout,我在一个版面上有三个编辑文本。当它是空的,然后我删除了正确的绘图。当它不是空的时候,我添加了可绘制的。当您单击“可绘制”时,我将清除编辑文本 当我单击rightdrawable时,光标被卡住,下划线保持彩色。我很少有2-3个光标。为什么?我做了什么坏事 坏的: 好: 布局: <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout

我在一个版面上有三个编辑文本。当它是空的,然后我删除了正确的绘图。当它不是空的时候,我添加了可绘制的。当您单击“可绘制”时,我将清除编辑文本

当我单击rightdrawable时,光标被卡住,下划线保持彩色。我很少有2-3个光标。为什么?我做了什么坏事

坏的:

好:

布局:

  <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/et3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@mipmap/ic_launcher"/>
助手类:

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;
        }
        }
    }
下一步:

公共类可提取状态{

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]);
        }
    }

}
我找到了解决办法

改变它

       if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) {
               ...
                return true;
            }
            return false;
致:

我找到了解决办法

改变它

       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;