hide default keyboard on click in android

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

    I got a good solution.I know its too late but when searching most of times getting this link as first link. so it may be helpful for others. If you click on any text/button it will hide the softkeyboard which is already visible.

    date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Hide soft keyboard
           InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
          // here i am showing the Date Dialog. one can proceed with their functionality
    
                //show date picker dialog
                showDialog(Date_DIALOG_ID);
            }
        });
    
    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
  • 2020-12-13 06:12

    Just set the input type to null like that

    editText.setInputType(InputType.TYPE_NULL);

    0 讨论(0)
  • 2020-12-13 06:16

    set inputType to zero for edit text
    editText.setInputType(0);

    it's work for me

    0 讨论(0)
提交回复
热议问题