每个人都知道要隐藏一个键盘,你需要实现:
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
但这里的大问题是如何隐藏键盘时,用户触摸或选择任何其他地方,不是一个EditText或softKeyboard?
我尝试在我的父活动上使用onTouchEvent(),但只有当用户在任何其他视图之外触摸并且没有滚动视图时才有效。
我试图实现一个触摸,点击,焦点监听器,但没有任何成功。
我甚至尝试实现我自己的滚动视图来拦截触摸事件,但我只能得到事件的坐标,而不是视图被单击。
有标准的方法来做这件事吗?在iPhone中,这非常简单。
一个更Kotlin和材料设计的方式使用TextInputEditText(这种方法也兼容EditTextView)…
1.通过添加以下属性,使父视图(您的活动/片段的内容视图)可单击和可聚焦
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
2.为所有View创建一个扩展(在ViewExtension内)。Kt文件为例):
fun View.hideKeyboard(){
val inputMethodManager = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(this.windowToken, 0)
}
3.创建一个继承了TextInputEditText的BaseTextInputEditText。实现onFocusChanged方法在视图未聚焦时隐藏键盘:
class BaseTextInputEditText(context: Context?, attrs: AttributeSet?) : TextInputEditText(context, attrs){
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect)
if (!focused) this.hideKeyboard()
}
}
4.只需在XML中调用全新的自定义视图:
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
...>
<com.your_package.BaseTextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
</android.support.design.widget.TextInputLayout>
这是所有。不需要修改你的控制器(片段或活动)来处理这种重复的情况。
我修改了Andre Luis IM的解决方案,我做到了这一点:
我创建了一个实用工具方法来隐藏软键盘,就像Andre Luiz IM做的那样:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
但不是注册一个OnTouchListener为每个视图,给一个糟糕的性能,我注册了OnTouchListener只为根视图。由于事件冒泡直到被消费(EditText是默认消费它的视图之一),如果它到达根视图,那是因为它没有被消费,所以我关闭软键盘。
findViewById(android.R.id.content).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Utils.hideSoftKeyboard(activity);
return false;
}
});
这对我来说是最简单的解决方案(也是我自己想出的)。
这是隐藏键盘的方法。
public void hideKeyboard(View view){
if(!(view instanceof EditText)){
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
}
现在将活动的父布局的onclick属性设置为上面的方法hideKeyboard,无论是从XML文件的Design视图还是在XML文件的Text视图中编写下面的代码。
android:onClick="hideKeyboard"
下面是fje回答的另一个变体,它解决了sosite提出的问题。
这里的思想是在Activity的dispatchTouchEvent方法中处理向下和向上操作。在按下操作时,我们会记录当前聚焦的视图(如果有的话),以及触摸是否在其中,并为以后保存这两个信息。
在向上操作中,我们首先分派,以允许另一个视图潜在地获得焦点。如果在那之后,当前聚焦的视图是最初聚焦的视图,向下触摸在那个视图内,然后我们让键盘打开。
如果当前聚焦视图与最初聚焦视图不同并且它是一个EditText,那么我们也让键盘打开。
否则我们关闭它。
综上所述,其工作原理如下:
when touching inside a currently focused EditText, the keyboard stays open
when moving from a focused EditText to another EditText, the keyboard stays open (doesn't close/reopen)
when touching anywhere outside a currently focused EditText that is not another EditText, the keyboard closes
when long-pressing in an EditText to bring up the contextual action bar (with the cut/copy/paste buttons), the keyboard stays open, even though the UP action took place outside the focused EditText (which moved down to make room for the CAB). Note, though, that when you tap on a button in the CAB, it will close the keyboard. That may or may not be desirable; if you want to cut/copy from one field and paste to another, it would be. If you want to paste back into the same EditText, it would not be.
when the focused EditText is at the bottom of the screen and you long-click on some text to select it, the EditText keeps focus and therefore the keyboard opens like you want, because we do the "touch is within view bounds" check on the down action, not the up action.
private View focusedViewOnActionDown;
private boolean touchWasInsideFocusedView;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
focusedViewOnActionDown = getCurrentFocus();
if (focusedViewOnActionDown != null) {
final Rect rect = new Rect();
final int[] coordinates = new int[2];
focusedViewOnActionDown.getLocationOnScreen(coordinates);
rect.set(coordinates[0], coordinates[1],
coordinates[0] + focusedViewOnActionDown.getWidth(),
coordinates[1] + focusedViewOnActionDown.getHeight());
final int x = (int) ev.getX();
final int y = (int) ev.getY();
touchWasInsideFocusedView = rect.contains(x, y);
}
break;
case MotionEvent.ACTION_UP:
if (focusedViewOnActionDown != null) {
// dispatch to allow new view to (potentially) take focus
final boolean consumed = super.dispatchTouchEvent(ev);
final View currentFocus = getCurrentFocus();
// if the focus is still on the original view and the touch was inside that view,
// leave the keyboard open. Otherwise, if the focus is now on another view and that view
// is an EditText, also leave the keyboard open.
if (currentFocus.equals(focusedViewOnActionDown)) {
if (touchWasInsideFocusedView) {
return consumed;
}
} else if (currentFocus instanceof EditText) {
return consumed;
}
// the touch was outside the originally focused view and not inside another EditText,
// so close the keyboard
InputMethodManager inputMethodManager =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
focusedViewOnActionDown.getWindowToken(), 0);
focusedViewOnActionDown.clearFocus();
return consumed;
}
break;
}
return super.dispatchTouchEvent(ev);
}
我知道这个线程是相当古老的,正确的答案似乎是有效的,有很多工作的解决方案,但我认为下面所述的方法可能有一个额外的好处,关于效率和优雅。
我的所有活动都需要这种行为,所以我创建了一个从类Activity继承的类CustomActivity,并“钩住”dispatchTouchEvent函数。主要有两个条件需要注意:
如果焦点没有改变,并且有人在当前输入域之外轻敲,则忽略输入法
如果焦点已经改变,并且下一个焦点元素不是任何类型的输入字段的实例,则驳回IME
这是我的结果:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_UP) {
final View view = getCurrentFocus();
if(view != null) {
final boolean consumed = super.dispatchTouchEvent(ev);
final View viewTmp = getCurrentFocus();
final View viewNew = viewTmp != null ? viewTmp : view;
if(viewNew.equals(view)) {
final Rect rect = new Rect();
final int[] coordinates = new int[2];
view.getLocationOnScreen(coordinates);
rect.set(coordinates[0], coordinates[1], coordinates[0] + view.getWidth(), coordinates[1] + view.getHeight());
final int x = (int) ev.getX();
final int y = (int) ev.getY();
if(rect.contains(x, y)) {
return consumed;
}
}
else if(viewNew instanceof EditText || viewNew instanceof CustomEditText) {
return consumed;
}
final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(viewNew.getWindowToken(), 0);
viewNew.clearFocus();
return consumed;
}
}
return super.dispatchTouchEvent(ev);
}
附注:另外,我将这些属性分配给根视图,使它能够清除每个输入字段的焦点,并防止输入字段获得活动启动的焦点(使内容视图为“焦点捕获器”):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = findViewById(R.id.content);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
}