Hide soft keyboard on losing focus

后端 未结 9 1253
-上瘾入骨i
-上瘾入骨i 2020-12-04 16:47

When we have an EditText and it loses focus (to an element that doesn\'t need a keyboard), should the soft keyboard hide automatically or are we supposed to hid

相关标签:
9条回答
  • 2020-12-04 17:04

    Try this, May be it will solve your problem.

    private void hideKeyboard() {
        InputMethodManager mImMan = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        mImMan.hideSoftInputFromWindow(mYourEdttxtName.getWindowToken(), 0);
    }
    

    You can find more information from here.

    0 讨论(0)
  • 2020-12-04 17:05

    This works for me:

    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
    0 讨论(0)
  • 2020-12-04 17:09

    You can override the dispatchTouchEvent method to achieve it:

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
    
            /**
             * It gets into the above IF-BLOCK if anywhere the screen is touched.
             */
    
            View v = getCurrentFocus();
            if ( v instanceof EditText) {
    
    
                /**
                 * Now, it gets into the above IF-BLOCK if an EditText is already in focus, and you tap somewhere else
                 * to take the focus away from that particular EditText. It could have 2 cases after tapping:
                 * 1. No EditText has focus
                 * 2. Focus is just shifted to the other EditText
                 */
    
                Rect outRect = new Rect();
                v.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                    v.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent( event );
    }
    

    Bonus: In case of an EditText gaining focus, the order of event triggered is:

    1. onFocusChange() of another EditText is called (if that other edittext is losing focus)
    2. ACTION_DOWN is called
    3. Finally, onFocusChange() method of that EditText will get called.
    0 讨论(0)
  • 2020-12-04 17:10

    Try this

     /**
     * Hide keyboard on touch of UI
     */
    public void hideKeyboard(View view) {
    
        if (view instanceof ViewGroup) {
    
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
    
                View innerView = ((ViewGroup) view).getChildAt(i);
    
                hideKeyboard(innerView);
            }
        }
        if (!(view instanceof EditText)) {
    
            view.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    hideSoftKeyboard(v);
                    return false;
                }
    
            });
        }
    
    }
    
    /**
     * Hide keyboard while focus is moved
     */
    public void hideSoftKeyboard(View view) {
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) contentsContext_
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputManager != null) {
                if (android.os.Build.VERSION.SDK_INT < 11) {
                    inputManager.hideSoftInputFromWindow(view.getWindowToken(),
                            0);
                } else {
                    if (this.getCurrentFocus() != null) {
                        inputManager.hideSoftInputFromWindow(this
                                .getCurrentFocus().getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    view.clearFocus();
                }
                view.clearFocus();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 17:12

    Best way is to set a OnFocusChangeListener for the EditText, and then add the code to the the keyboard into the OnFocusChange method of the listener. Android will then automatically close the keyboard when the EditText loses focus.

    Something like this in your OnCreate method:

    EditText editText = (EditText) findViewById(R.id.textbox);
    OnFocusChangeListener ofcListener = new MyFocusChangeListener();
    editText.setOnFocusChangeListener(ofcListener);
    

    and then add the class:

    private class MyFocusChangeListener implements OnFocusChangeListener {
    
        public void onFocusChange(View v, boolean hasFocus){
    
            if(v.getId() == R.id.textbox && !hasFocus) {
    
                InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 17:20

    Android will not hide the keyboard for you. If you want the keyboard to hide when your EditText loses focus, try using a method like this on that event:

    private void hideKeypad() {
        EditText edtView = (EditText) findViewById(R.id.e_id);
    
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edtView.getWindowToken(), 0);
    }
    
    0 讨论(0)
提交回复
热议问题