Hide the keyboard

Asked

Viewed 13,518 times

5

As soon as the user clicks on one of EditText from my Android app, the keyboard appears, however, it does not disappear when you finish typing and click off it.

I would like to know which method would be appropriate, knowing that I have 3 EditText.

I have tried to use all the following methods, but none with success:

https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard

Edit.: The class code is here.

The code line corresponding to the error after the change is:

editRed.setOnFocusChangeListener(new OnFocusChangeListener()
  • The link is broken, could you better support the community, enter the code in context?! this can help many other people too.

5 answers

13


I believe that clicking outside Edittext will not make the virtual keyboard disappear. You need to implement a System that hides the keyboard when you click out of it (that is, when Edittext loses focus). For example:

    searchEditText.setOnFocusChangeListener(new OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus)
        {
            if (false == hasFocus) {
                ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        searchEditText.getWindowToken(), 0);
            }
        }
    });
  • searchEditText would be what then?

  • A reference to your Edittext, for example: EditText searchEditText = (EditText)findViewById(R.id.searchEditText); the name is just an example.

  • Ah yes, right, and the context?

  • The context can be removed, I took an example from the link you quoted. Use this.getSystemService() for example.

  • I refer to "context.getSystemService".

  • I corrected the comment.

  • I believe you can change searchEditText.getWindowToken() for ((EditText)v).getWindowToken(), try it.

  • When replacing "context" with "this", the error "The method getSystemService(String) is Undefined for the type new View.Onfocuschangelistener(){}".

  • Ah yes. It was wrong. Do so: SuaAtividade.this.getSystemService( or maybe just remove the this that should work.

  • I created a Lerner for each Edit, made the necessary changes, but at the time of the test gave Nullpointerexception. See: http://pastebin.com/PAvdRXNk

  • Include your Calculator.java in the question and tell us which is the line 50, which is where the error is.

  • Edited. I apologize for the delay, I could not enter the computer since yesterday.

  • Your variables editRed, editGreen, editBlue are not initialized. Before that line and after setContentView() you must include three lines according to this example: editRed = (EditText)findViewById(R.id.edtRed);. Move the lines you have on getCode(), there is not the right place to initialize these variables.

  • It worked, thank you!

  • Thanks friend, saved here.!!!

Show 10 more comments

2

Utility method I use to do this:

public static void hideKeyboard(Context context, View editText) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

0

I also had this problem and solved setting Relativelayout with an id and using this code:

Relativo.setOnTouchListener(new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

                ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        Relativo.getWindowToken(), 0);
            return false;
        }

    });

-1

You can put the entire screen inside a Gesturedetector

new Scaffold(
 body: new GestureDetector(
   onTap: () {
    FocusScope.of(context).requestFocus(FocusNode());
  },
 child: Container()
 )
)

-1

There are two ways to make the keyboard disappear:

1st form

  • make Edittext active to which you want the keyboard to disappear after the command ;
  • research the Attribute imeOptions;
  • open the options range by clicking the left arrow;
  • select the actionDone option for TRUE
  • save the change and execute.

2nd form

  • select the tab activity_main.xml;
  • activate the CODE tab view;
  • insert the text android:imeOptions="actionDone" on a tag line where you want the keyboard to close.
  • save the change and execute.

Browser other questions tagged

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