我有一个EditText
电话myTextview
.我希望软键盘在我点击时显示,EditText
但如果我点击外面则解雇EditText
.所以我使用下面的方法.但是当我在视图外面点击时键盘不会被忽略(我点击了一下TextView
).我该如何修复此代码?
myTextview.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); } else { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); } } });
Felipe R. Sa.. 52
我找到了更好的解决方案:
覆盖dispatchTouchEvent
Activity中的方法.
@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); } 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); } }
即使您正在使用webview,这也适用.
也许有点容易:
在编辑文本上设置focusChangedListener,然后在没有焦点时隐藏键盘.
yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ hideKeyboard(); } } }); private void hideKeyboard() { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); }
我找到了更好的解决方案:
覆盖dispatchTouchEvent
Activity中的方法.
@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); } 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); } }
即使您正在使用webview,这也适用.