Android软触摸键盘弹出后无法关闭问题求助
Hey there! I get it—having a soft keyboard that won't close even when you tap the close button is super frustrating, especially when you're just starting out with Android dev. Since your EditText is set to stay focused at all times, that's almost certainly why the system refuses to hide the keyboard. Let's walk through a few straightforward fixes:
1. Let the EditText lose focus when tapping outside the input field
The keyboard stays open because the EditText is clinging to focus. To fix this, we'll make the root layout of your activity focusable, and add a touch listener to clear focus from the EditText when the user taps anywhere else:
First, update your root layout in XML to allow it to take focus:
<!-- Your root layout (e.g., LinearLayout, ConstraintLayout) --> <LinearLayout ... android:focusable="true" android:focusableInTouchMode="true">
Then, add this code in your Activity's onCreate method:
// Replace R.id.root_layout with your actual root layout ID View rootLayout = findViewById(R.id.root_layout); EditText yourInput = findViewById(R.id.your_edit_text); // Your input field rootLayout.setOnTouchListener((v, event) -> { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); // Hide the keyboard imm.hideSoftInputFromWindow(yourInput.getWindowToken(), 0); // Let the EditText lose focus yourInput.clearFocus(); return false; });
2. Add a "Done" button to the keyboard and handle its click
Sometimes the default close button doesn't play nice with a permanently focused EditText. Adding an explicit "Done" action to your keyboard gives users another way to dismiss it, and we can hook into that action to clear focus:
First, update your EditText's XML attributes:
<EditText ... android:imeOptions="actionDone" android:inputType="text" /> <!-- Use your desired input type here -->
Then, add an editor action listener in your Activity:
yourInput.setOnEditorActionListener((v, actionId, event) -> { if (actionId == EditorInfo.IME_ACTION_DONE) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); yourInput.clearFocus(); return true; } return false; });
3. Override the back button to hide the keyboard
As a fallback, you can make the device's back button explicitly dismiss the keyboard first before handling the usual back action:
@Override public void onBackPressed() { EditText yourInput = findViewById(R.id.your_edit_text); InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); if (imm.isActive(yourInput)) { // If the keyboard is open for this EditText, hide it and clear focus imm.hideSoftInputFromWindow(yourInput.getWindowToken(), 0); yourInput.clearFocus(); } else { // Otherwise, proceed with normal back button behavior super.onBackPressed(); } }
The key takeaway here is that the keyboard stays open because your EditText never releases focus. By giving it a way to lose focus and adding explicit keyboard handling, you should get that close button working as expected.
内容的提问来源于stack exchange,提问作者user5864910




