我在一个布局上有三个edittext。如果它是空的,那么我删除了右侧的drawable。当它不空时,我添加了drawable。当你点击可绘制时,我清除了edittext。
当我点击向右箭头时,光标被卡住了,下划线保持着色。我很少有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"/>
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]);
}
}
}
答案 0 :(得分:0)
我找到了解决方案。
改变它
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;