最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - I want to prevent the software keyboard from appearing in Android applications - Stack Overflow

programmeradmin6浏览0评论

I am developing an Android application in Java in Android Studio.The environment is as follows

Environment minSdkVersion: 29 targetSdkVersion: 35 Device: Android device with physical keys Use of custom EditTextStyle: Yes

The process of screen transition is implemented by clicking a button. I want to prevent the software keyboard from being displayed when EditText has the focus after the screen transition. setShowSoftInputOnFocus(false) is set, but the keyboard is displayed under the following conditions

The software keyboard is displayed only at the first input immediately after the Enter key screen transition on the physical keyboard. (The software keyboard is not displayed when the screen is transitioned by tapping the button on the screen.) After typing once, it hides normally. What we are looking for

How to make sure that the software keyboard is not displayed when the screen transitions and the first time input is made, even if EditText has the focus. In particular, a way to achieve the same behavior when the screen transitions with the Enter key on the physical keyboard. If possible, a systematic solution rather than hard-coding

The contents are converted from Japanese to English at DeepL.

I am developing an Android application in Java in Android Studio.The environment is as follows

Environment minSdkVersion: 29 targetSdkVersion: 35 Device: Android device with physical keys Use of custom EditTextStyle: Yes

The process of screen transition is implemented by clicking a button. I want to prevent the software keyboard from being displayed when EditText has the focus after the screen transition. setShowSoftInputOnFocus(false) is set, but the keyboard is displayed under the following conditions

The software keyboard is displayed only at the first input immediately after the Enter key screen transition on the physical keyboard. (The software keyboard is not displayed when the screen is transitioned by tapping the button on the screen.) After typing once, it hides normally. What we are looking for

How to make sure that the software keyboard is not displayed when the screen transitions and the first time input is made, even if EditText has the focus. In particular, a way to achieve the same behavior when the screen transitions with the Enter key on the physical keyboard. If possible, a systematic solution rather than hard-coding

The contents are converted from Japanese to English at DeepL.

Share asked Apr 1 at 3:30 らむねらむね 1 New contributor らむね is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • stackoverflow/questions/1109022/… – vbijoor Commented Apr 1 at 4:59
  • This question is similar to: How can I close/hide the Android soft keyboard programmatically?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – TechyBenji Commented Apr 1 at 6:57
Add a comment  | 

1 Answer 1

Reset to default 1

Step 1: In your EditText, disable soft keyboard on focus

editText.setShowSoftInputOnFocus(false);

Step 2: In the AndroidManifest.xml, hide the keyboard when the activity starts

Set this flag for the activity you're transitioning to:

<activity
    android:name=".TargetActivity"
    android:windowSoftInputMode="stateHidden|adjustResize" />

Step 3: (Optional but robust) Force-hide the keyboard in onResume() of the target activity

Just in case any edge case slips through (e.g. from physical Enter key), add this:

@Override
protected void onResume() {
    super.onResume();
    View view = getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

Alternative solution is like : Delay Focus Handling to Avoid Initial Keyboard Trigger
If keyboard still appears due to system race conditions, delay focus handling like this:

editText.setShowSoftInputOnFocus(false);
editText.clearFocus(); // ensure it's not focused prematurely

new Handler(Looper.getMainLooper()).postDelayed(() -> {
    editText.requestFocus();
}, 100); // slight delay to bypass the default system behavior
发布评论

评论列表(0)

  1. 暂无评论