简体   繁体   中英

Strange Android Soft Keyboard Issue

I'm writing an activity, which has lots of EditText.

Their inputType are numericDecimal. Like this: Before I click

Now, I want to hide the soft keyboard when clicking somewhere other than the EditTexts, so I put:

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. 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.

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. It will make sure that whenever you click out of your editText it will close the keyboard.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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