hide default keyboard on click in android

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

    This can be done using following code :

    1) Take a reference of your parent layout into java code by using findViewById().

    2) then apply setOnTouchListener() to it.

    3) Add following code in onTouchMethod().

     lin = (LinearLayout) findViewById(R.id.lin);
        lin.setOnTouchListener(new OnTouchListener() 
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                   imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
                           return false;
            }
        });
    

提交回复
热议问题