Auto-float keyboard on first run of Android activity

Asked 2 years ago, Updated 2 years ago, 23 views

If you give the stateVisable or stateAlwaysVisable attribute in windowsSoftInputMode, the keyboard automatically rises even when the activity goes out to the home screen and then turns on the app again...

I only want to display the keyboard when I first run the app.

    @Override
    protected void onStart() {
        searchBox.requestFocus();
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // // im.showSoftInput(searchBox, 0);
        im.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        super.onStart();
    }

I tried it this way, but the keyboard didn't come up in this way. The code for displaying the keyboard itself seems to have no problem as the keyboard comes up normally when you register the onClickListener with the button and test it...

How do I get my keyboard up only when I first run the app...?

android

2022-09-22 19:03

1 Answers

Unfortunately, if you are creating activity, the request seems to be ignored. The moment activity is shown on the screen, life cycle onCreate -> onStart -> onResume must all end. Therefore, you will need to place the code on Resume or give me a delay to request it.

searchBox.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 
        }
    },200);

Try putting this code in onStart() or onCreat().


2022-09-22 19:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.