我有一个EditText,右边有一个复合drawable。当我按下drawable并清除文本时,我想要隐藏软键盘。为此,我有以下代码:
filterText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (filterText.getCompoundDrawables()[2] == null) {
// cross is not being shown so no need to handle
return false;
}
if (event.getAction() != MotionEvent.ACTION_DOWN) {
// only respond to the down type
return false;
}
if (event.getX() > filterText.getMeasuredWidth() -
filterText.getPaddingRight() - d.getIntrinsicWidth()) {
filterText.setText("");
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return false;
}
else {
return true;
}
}
});
但它不起作用,因为editText似乎保持焦点。我试过过filterText.clearFocus,但没办法。
由于
答案 0 :(得分:1)
只是一个建议:你不能在Button
的右侧放置EditText
吗?