gpt4 book ai didi

java - Android Edittext Drawable 点击卡住焦点

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:45 25 4
gpt4 key购买 nike

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

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

坏: enter image description here

好:enter image description here

布局:

  <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"/>

主要 Activity 公共(public)类 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;
}
}
}

下一个:

公共(public)类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]);
}
}

}

最佳答案

我找到了解决方案。

改变它

       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;

关于java - Android Edittext Drawable 点击卡住焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com