简体   繁体   English

奇怪的Android软键盘问题

[英]Strange Android Soft Keyboard Issue

I'm writing an activity, which has lots of EditText. 我正在写一个活动,其中包含许多EditText。

Their inputType are numericDecimal. 它们的inputType是numericDecimal。 Like this: Before I click 像这样: 在我点击之前

Now, I want to hide the soft keyboard when clicking somewhere other than the EditTexts, so I put: 现在,我想在单击EditTexts以外的地方时隐藏软键盘,所以我输入:

public void hideKeyboard(View mView) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService
            (Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

After I click somewhere else, the numericDecimal soft keyboard do disappear. 单击其他位置后,numericDecimal软键盘确实消失了。 HOWEVER , there is still a common soft keyboard without autocompletion left on the screen, and I have absolutely no clue where does this come from. 但是 ,仍然有一个普通的软键盘,屏幕上没有自动补全功能,我绝对不知道这是从哪里来的。 Showing here: After I click 在这里显示: 点击后

So how to hide them all? 那么如何隐藏它们呢? Common ways on Internet don't work, I tried them all. Internet上的常见方法不起作用,我尝试了所有方法。

Thanks in advance! 提前致谢!

Try this code : 试试这个代码:

public static void setupUI(final View view, final Activity activity) {

    if (view instanceof ViewGroup) {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Utils.hideSoftKeyboard(activity, view);
                return false;
            }
        });
    }

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView, activity);
        }
    }
}

    public static void hideSoftKeyboard(Activity activity, View searchET) {
    try {
        InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(searchET.getWindowToken(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Call the setupUI function and pass the parent Layout in your activity. 调用setupUI函数并在您的活动中传递父级Layout。 It will make sure that whenever you click out of your editText it will close the keyboard. 这将确保每当您单击editText时,它将关闭键盘。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM