简体   繁体   中英

show soft keyboard onResume

Couldn't find a clear answer to this one, basically I have an activity with an EditText field. The soft keyboard is set to visible within manifest, so keyboard is visible when activity starts however if user navigates away and returns using the back button the keyboard is hidden (I need it visible on resume). I have added the below method to my onResume but doesn't seem to work? Any ideas what I'm missing here?

private void showSoftKeyboard(){
    quickListName.requestFocus();
    InputMethodManager imm = D(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(quickListName,InputMethodManager.SHOW_IMPLICIT);
}

尝试这个:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Previously, I had used below code inside the onResume() method and the soft keyboard showed up if only the onPause() method was called for this activity and I came back to this activity. But there was a situation where onStop() method for this activity was invoked. When I returned to this activity again, onResume() gets called but the soft keyboard was not displayed.

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(this.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);

I used the following code in the onResume() method instead of the one mentioned above to show the soft key when onStop() for this activity was also invoked.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

当您收到onStop回调时,尝试在EditText上调用clearFocus

try { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } catch (Exception e) { e.printStackTrace(); }

Try this :

override fun onResume() {
    super.onResume()
    val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

override fun onPause() {
    super.onPause()
    val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

This forces the keyboard to open in the onResume() method and closes it in the onPause() method.

You should not try to show keyboard from a fragment's onResume. Using InputMethodManager.toggleSoftInput is a hack, doesn't work on Android 11 (R), and you don't know immediately whether the keyboard will show or not.

Why isn't the keyboard showing?

When an activity in a window is just launched (including one returning from the background), the window doesn't immediately get marked as focused. When you call InputMethodManager.showSoftInput inside onResume , it will return false, because although the view you are trying to show the keyboard from may be focused, it is still inside a window that isn't. Thus the keyboard won't show up.

What's the right way to do this?

The correct way is to override Activity.onWindowFocusChanged and either pass it to your fragment, or show the keyboard directly from there. Here's a snippet for the latter:

@Override
public void onWindowFocusChanged(boolean isFocused) {
  if (!isFocused) {
    return;
  }
  InputMethodManager inputMethodManager = 
      (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  inputMethodManager.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM