简体   繁体   中英

Why doesn't EditText lose focus after soft keyboard is hidden?

How does EditText still have focus after the soft keyboard is hidden (user presses Done/Enter/Search button)?

Could you please provide me some explanation about this?

That's the default Android behaviour. You can call view.clearFocus() on Enter key press to override this.

Your app and Android soft keyboard ( stock one ) are different app processes. And while Android takes care of "notifying" the soft keyboard app on your app's EditText focus requests , neither Android nor the soft keyboard app should be responsible for your app's EditText focus changes . Your app has enough callbacks from the soft keyboard at its disposal to clear focus.

- Add Following Line of Code in your Specific Activity.

EditText edt_user = findViewById(R.id.edt_user);      
    InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
        imm.showSoftInput(edt_user,0);

    edt_user.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                edt_user.clearFocus();
            }
            return false;
        }
    });

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