单击外部EditText后如何在android上隐藏软键盘? [英] How to hide soft keyboard on android after clicking outside EditText?

查看:26
本文介绍了单击外部EditText后如何在android上隐藏软键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,每个人都知道要隐藏键盘,您需要实现:

Ok everyone knows that to hide a keyboard you need to implement:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

但这里的重要问题是当用户触摸或选择不是 EditText 或 softKeyboard 的任何其他位置时如何隐藏键盘?

But the big deal here is how to hide the keyboard when the user touches or selects any other place that is not an EditText or the softKeyboard?

我尝试在我的父 Activity 上使用 onTouchEvent() 但这仅在用户触摸任何其他视图之外并且没有滚动视图时才有效.

I tried to use the onTouchEvent() on my parent Activity but that only works if user touches outside any other view and there is no scrollview.

我尝试实现一个触摸、点击、聚焦监听器,但没有成功.

I tried to implement a touch, click, focus listener without any success.

我什至尝试实现自己的滚动视图来拦截触摸事件,但我只能获取事件的坐标,而不能获取点击的视图.

I even tried to implement my own scrollview to intercept touch events but I can only get the coordinates of the event and not the view clicked.

有没有标准的方法来做到这一点?在 iPhone 中,这真的很容易.

Is there a standard way to do this?? in iPhone it was really easy.

推荐答案

以下代码段只是隐藏了键盘:

The following snippet simply hides the keyboard:

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = 
        (InputMethodManager) activity.getSystemService(
            Activity.INPUT_METHOD_SERVICE);
    if(inputMethodManager.isAcceptingText()){
        inputMethodManager.hideSoftInputFromWindow(
                activity.getCurrentFocus().getWindowToken(),
                0
        );
    }
}

您可以将其放在实用程序类中,或者如果您在活动中定义它,请避免使用活动参数,或调用 hideSoftKeyboard(this).

You can put this up in a utility class, or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

最棘手的部分是何时调用它.您可以编写一个方法来遍历活动中的每个 View,并检查它是否是 instanceof EditText 如果它没有注册 setOnTouchListener到那个组件,一切都会就位.如果您想知道如何做到这一点,实际上很简单.这就是你所做的,你写了一个像下面这样的递归方法,实际上你可以用它来做任何事情,比如设置自定义字体等......这是方法

The trickiest part is when to call it. You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following, in fact you can use this to do anything, like setup custom typefaces etc... Here is the method

public void setupUI(View view) {

    // Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(MyActivity.this);
                return false;
            }
        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}

就是这样,只需在您的活动中 setContentView 之后调用此方法即可.如果您想知道要传递什么参数,它是父容器的 id.将 id 分配给您的父容器,例如

That is all, just call this method after you setContentView in your activity. In case you are wondering what parameter you would pass, it is the id of the parent container. Assign an id to your parent container like

... </RelativeLayout>

并调用setupUI(findViewById(R.id.parent)),就是这样.

如果你想有效地使用它,你可以创建一个扩展的Activity并把这个方法放进去,并使你应用程序中的所有其他活动扩展这个活动并调用它的setupUI()onCreate() 方法中.

If you want to use this effectively, you may create an extended Activity and put this method in, and make all other activities in your application extend this activity and call its setupUI() in the onCreate() method.

希望有帮助.

如果您使用 1 个以上的活动,请为父布局定义通用 ID,例如<RelativeLayout android:id="@+id/main_parent">... </RelativeLayout>

If you use more than 1 activity define common id to parent layout like <RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>

然后从 Activity 扩展一个类并在其 OnResume() 内定义 setupUI(findViewById(R.id.main_parent)) 并扩展这个类而不是你程序中的``Activity

Then extend a class from Activity and define setupUI(findViewById(R.id.main_parent)) Within its OnResume() and extend this class instead of ``Activity in your program

这是上述函数的 Kotlin 版本:

Here is a Kotlin version of the above function:

@file:JvmName("KeyboardUtils")

fun Activity.hideSoftKeyboard() {
    currentFocus?.let {
        val inputMethodManager = ContextCompat.getSystemService(this, InputMethodManager::class.java)!!
        inputMethodManager.hideSoftInputFromWindow(it.windowToken, 0)
    }
}

这篇关于单击外部EditText后如何在android上隐藏软键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆