【发布时间】:2017-03-27 10:52:20
【问题描述】:
所以,我有一个EditText,当您在EditText 外部单击时,我有一个隐藏键盘的功能。接下来会发生什么:
- 第一次选择
EditText,键盘出现并将整个View向上推。 - 我取消选择
EditText(单击EditText的任何其他位置),键盘进入隐藏状态 - 当我再次单击
EditText时,键盘出现,但View没有拉起,我看不到我的EditText
当EditText被选中时,如何再次向上推该视图?
这里是隐藏软键盘的代码:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
View v = getCurrentFocus();
if (v != null &&
(ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
v instanceof EditText &&
!v.getClass().getName().startsWith("android.webkit.")) {
int scrcoords[] = new int[2];
v.getLocationOnScreen(scrcoords);
float x = ev.getRawX() + v.getLeft() - scrcoords[0];
float y = ev.getRawY() + v.getTop() - scrcoords[1];
if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
hideKeyboard(this);
}
return super.dispatchTouchEvent(ev);
}
和hideKeyboard() 方法
public static void hideKeyboard(Activity activity) {
if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
}
}
【问题讨论】:
-
尝试使用内置方法。这里是您的解决方案stackoverflow.com/questions/43042489/…
-
已经尝试过 adjust_resize 和该捆绑包中的其余部分..
-
是的,从清单中的活动内置方法和 onCreate 代码中尝试了所有方法。
标签: android android-edittext keyboard android-input-method android-textinputedittext