我有以下形式的 Activity 布局
<relativelayout
<scrollview
<linearlayout
<EditText1
<EditText2
<EditText3
</linearlayout>
</scrollview>
</relativelayout>
每当我点击出现的任何编辑文本键盘时,我都需要实现以下设置
在编辑文本之外单击时键盘应该消失 (在 dispatchTouchEvent() 中关闭键盘时工作正常 方法)。
键盘不应在用户滚动时消失。 (目前正在消失)
请指导。
编辑 -1 添加了消除 ke 的代码
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
View view = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (view instanceof EditText)
{
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()))
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}`
最佳答案
您应该根据编辑文本的焦点来处理它。我认为这可能会有所帮助:
EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// code to execute when EditText loses focus
}
}
});
关于Android - 滚动时防止键盘消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29598953/