Hide keyboard with Fragment

0

I'm doing a mobile application that has screens in fragment with Drawer, and they have EditText , but I'm having a problem that when I click on EditText and then click off, or click on menu does not disappear, and it is difficult to see both the rest of the form and the Drawer menu.

And if I go back to Home where it does not have EditText it continues on the keyboard screen. How do I close it?

    
asked by anonymous 07.11.2014 / 19:19

2 answers

1

Try to create a listener to run when the editText focus 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: link

    
09.11.2014 / 15:53
0

In onCreateView puts the following code to hide the keyboard.

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

That is:

    @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;
          }
    
10.11.2014 / 18:35