A
A
Andrey Smirnov2018-03-18 21:57:00
Java
Andrey Smirnov, 2018-03-18 21:57:00

How to hide virtual keyboard and EditText at the same time?

Essence of the question: there is an activity, on it an EditText (invisible) and a button, when pressed, the EditText becomes visible and a virtual keyboard pops up. Clicking on Back hides the keyboard and keeps the EditText visible and populated. When you click on Back again, the EditText is hidden and cleared, and the idea is to hide them at the same time. Please tell me what's the matter and how to deal with it, thanks in advance.
/////////////////////////////////////////////////// ///////////
editText.setVisibility(View.VISIBLE);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
/////////////////////////////////////////////////// /////////////////
@Override
public void onBackPressed() {
//super.onBackPressed();
editText.setText(null);
editText.setFocusable(false);
editText.setVisibility(View.INVISIBLE);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Marken, 2018-03-19
@AlexMarken

Try the code below. Call the method after the focus is cleared and the element is hidden.
The method works in both Activity and Fragment.

public void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question