hide default keyboard on click in android

后端 未结 10 830
忘掉有多难
忘掉有多难 2020-12-13 05:52

i want to hide the soft keyboard when i click out side of editbox in a screen. how can i do this?

10条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 06:10

    public boolean OutsideTouchEvent(MotionEvent m_event) {
        View v = getCurrentFocus();
        boolean value = super.dispatchTouchEvent(m_event);
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = m_event.getRawX() + w.getLeft() - scrcoords[0];
        float y = m_event.getRawY() + w.getTop() - scrcoords[1];
    
        if (m_event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { 
            InputMethodManager inputMethodManager = (InputMethodManager)  YourActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(YourActivity.this.getCurrentFocus().getWindowToken(), 0);
        }
        return value;
    
    }
    

提交回复
热议问题