Hide keyboard with Fragment

Asked

Viewed 947 times

0

I’m making a mobile app that has screens on Fragment with Drawer, and in them EditText, but I’m having a problem that by clicking on EditText and then click away, or click the menu the keyboard does not disappear, and disturbs viewing both the rest of the form and the Drawer menu.

And if I go back home where there isn’t EditText it continues on the screen the keyboard. How to close?

  • I had this problem too, I used this [tip][1]
 
 
 [1]: http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard

2 answers

1

Try creating a Listener to run when the editText is removed. Something like this:

EditText editText = (EditText) findViewById(R.id.textbox);
EditText editText2 = (EditText) findViewById(R.id.textbox2);
EditText editText3 = (EditText) findViewById(R.id.textbox3);

OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);
editText2.setOnFocusChangeListener(ofcListener);
editText3.setOnFocusChangeListener(ofcListener);
private class MyFocusChangeListener implements OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus){

        if(!v.hasFocus) {

            InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

        }
    }
}

Source: https://stackoverflow.com/questions/15412943/hide-soft-keyboard-on-losing-focus

  • I happen to have more than one Edittext, so I can’t get it right.

  • 1

    I made some changes to several Views, in case you arrow the Editor for all your Edittext, in the example I set to 3, but you can do this dynamically if you have an X number of views.

0

onCreateView puts the following code to hide the keyboard.

getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

That is to say:

    @Override
     public View onCreateView(final LayoutInflater inflater,
                        final ViewGroup container, Bundle savedInstanceState)
          {

            view = inflater.inflate(R.layout.fragment_mensal, container,false);     

//esconder teclado
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

                    return view;
          }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.