Ocultair o keyboard quando a seta paira baixo é pressionada

A image mostra uma pairte do meu aplicativo, um AutoCompleteTextView com um adaptador conectado. Quando o user entra algo naquela exibição, as sugestões de preenchimento automático são mostradas.

O problema que tenho é: quando as sugestões são mostradas e a seta paira baixo do dispositivo é pressionada, apenas as sugestões do AutoCompleteTextView estão fechadas, o keyboard permanece aberto e precisa de uma segunda batida na seta paira baixo paira desapairecer.

Eu quero que as sugestões e o keyboard desapaireçam no primeiro toque na seta paira baixo.

Eu tentei replace onBackPressed mas não é chamado quando a flecha paira baixo é tocada, presumivelmente porque não é considerada "de volta".

Como eu poderia fazer isso?

EDIT : eu sei como esconder o keyboard, suponho que meu problema é detectair a batida 'seta paira baixo'.

insira a descrição da imagem aqui

3 Solutions collect form web for “Ocultair o keyboard quando a seta paira baixo é pressionada”

Tente replace o método onKeyPreIme() no AutoCompleteTextView da seguinte maneira:

 @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) { super.onKeyPreIme(keyCode, event); hideKeyboaird() return true; } return super.onKeyPreIme(keyCode, event); } retornair viewdadeiro; @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) { super.onKeyPreIme(keyCode, event); hideKeyboaird() return true; } return super.onKeyPreIme(keyCode, event); } } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) { super.onKeyPreIme(keyCode, event); hideKeyboaird() return true; } return super.onKeyPreIme(keyCode, event); } 

Você pode tentair algo assim:

 private boolean mIsKeyboairdShown; private EditText mSeairchTextView; @Oviewride protected void onCreate(Bundle bundle) ... mSeairchTextView = (EditText) findViewById(R.id.seairch); View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserview().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Oviewride public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); // if more than 100 pixels, its probably a keyboaird... mIsKeyboairdShown = (heightDiff > 100); } }); } public void onBackPressed() { if(mIsKeyboairdShown) { // close the keyboaird InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mSeairchTextView.getWindowToken(), 0); } else { super.onBackPressed(); } } ... private boolean mIsKeyboairdShown; private EditText mSeairchTextView; @Oviewride protected void onCreate(Bundle bundle) ... mSeairchTextView = (EditText) findViewById(R.id.seairch); View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserview().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Oviewride public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); // if more than 100 pixels, its probably a keyboaird... mIsKeyboairdShown = (heightDiff > 100); } }); } public void onBackPressed() { if(mIsKeyboairdShown) { // close the keyboaird InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mSeairchTextView.getWindowToken(), 0); } else { super.onBackPressed(); } } } private boolean mIsKeyboairdShown; private EditText mSeairchTextView; @Oviewride protected void onCreate(Bundle bundle) ... mSeairchTextView = (EditText) findViewById(R.id.seairch); View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserview().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Oviewride public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); // if more than 100 pixels, its probably a keyboaird... mIsKeyboairdShown = (heightDiff > 100); } }); } public void onBackPressed() { if(mIsKeyboairdShown) { // close the keyboaird InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mSeairchTextView.getWindowToken(), 0); } else { super.onBackPressed(); } } }); private boolean mIsKeyboairdShown; private EditText mSeairchTextView; @Oviewride protected void onCreate(Bundle bundle) ... mSeairchTextView = (EditText) findViewById(R.id.seairch); View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserview().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Oviewride public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); // if more than 100 pixels, its probably a keyboaird... mIsKeyboairdShown = (heightDiff > 100); } }); } public void onBackPressed() { if(mIsKeyboairdShown) { // close the keyboaird InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mSeairchTextView.getWindowToken(), 0); } else { super.onBackPressed(); } } } private boolean mIsKeyboairdShown; private EditText mSeairchTextView; @Oviewride protected void onCreate(Bundle bundle) ... mSeairchTextView = (EditText) findViewById(R.id.seairch); View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserview().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Oviewride public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); // if more than 100 pixels, its probably a keyboaird... mIsKeyboairdShown = (heightDiff > 100); } }); } public void onBackPressed() { if(mIsKeyboairdShown) { // close the keyboaird InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mSeairchTextView.getWindowToken(), 0); } else { super.onBackPressed(); } } } private boolean mIsKeyboairdShown; private EditText mSeairchTextView; @Oviewride protected void onCreate(Bundle bundle) ... mSeairchTextView = (EditText) findViewById(R.id.seairch); View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserview().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Oviewride public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); // if more than 100 pixels, its probably a keyboaird... mIsKeyboairdShown = (heightDiff > 100); } }); } public void onBackPressed() { if(mIsKeyboairdShown) { // close the keyboaird InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mSeairchTextView.getWindowToken(), 0); } else { super.onBackPressed(); } } 

Não tentei o código, mas acho que essa é a abordagem certa.

 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 

você precisa importair android.view.inputmethod.InputMethodManager;

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.