hide default keyboard on click in android

后端 未结 10 816
忘掉有多难
忘掉有多难 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:04

    First of all thank you to Daniel, his code is really nice and I was using it for a while.

    Recently I realized that I have to improve it. The problem was scrolling page. I had many EditTexts in my project and it was hiding the keyboard when you scroll the page.

    I came up with a solution using onGestureListener instead of overriding dispatchTouchEvent.

    public class TabActivity extends ActionBarActivity implements GestureDetector.OnGestureListener {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            ...
            gestureScanner = new GestureDetector(TabActivity.this,this);
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            gestureScanner.onTouchEvent(ev);
            return super.dispatchTouchEvent(ev);
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            View v = getCurrentFocus();
    
            if (v instanceof EditText) {
                View w = getCurrentFocus();
                int scrcoords[] = new int[2];
                w.getLocationOnScreen(scrcoords);
                boolean hide = true;
    
                View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
                ArrayList editTexts = view.getFocusables(0);     // Get All EditTexts in view
    
                for(int i=0; i< editTexts.size(); i++){
                    View editText = editTexts.get(i);
                    editText.getLocationOnScreen(scrcoords);
                    float x = event.getRawX();
                    float y = event.getRawY();
                    int viewX = scrcoords[0];
                    int viewY = scrcoords[1];
    
                    // If touch is in any of EditText, keep keyboard active, otherwise hide it.
                    if (event.getAction() == MotionEvent.ACTION_UP  && ( x > viewX && x < (viewX + editText.getWidth())) && ( y > viewY && y < (viewY + editText.getHeight())) ) {
                        hide = false;
                    }
                }
    
                if (hide) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
                }
            }
            return true;
        }
    
        @Override
        public boolean onScroll(MotionEvent event, MotionEvent e2, float distanceX, float distanceY) {
            return true;
        }     
    }
    

    So, if user scrolls the page it goes to onScroll method and it does nothing. If users just touches to screen it triggers onSingleTapUp method.

    I also had to change if statement of Daniel's code. Daniel was checking if the touch event is outside the EditText. Since I have many EditViews I changed the code to find if touch event is inside any of EditTexts.

    It works fine with me, let me know for any kind of improvements or mistakes.

提交回复
热议问题