Android click to close the keyboard

Source: Internet
Author: User

Android click to close the keyboard

In the project, after editText gets the focus, a soft keyboard is automatically displayed. When it is disabled, you usually need to press the back key or click the button on the soft keyboard,

Even if the current activity has been completed, the soft keyboard still exists, which will affect the user experience.

There are currently many detailed methods on the Internet, such as clicking on other blank areas, and the soft keyboard will disappear. This is not required in our project, as long

Other operations are not blocked, and the keyboard disappears after the current Activity is disabled,

We will share with you two methods today:

 

// This method is hidden if it is displayed. If it is hidden, private void hintKbOne () {InputMethodManager imm = (InputMethodManager) getActivity () is displayed (). getSystemService (Context. INPUT_METHOD_SERVICE); // obtain the InputMethodManager instance if (imm. isActive () {// If you enable imm. toggleSoftInput (InputMethodManager. SHOW_IMPLICIT, InputMethodManager. HIDE_NOT_ALWAYS );}}

// This method only disables the soft keyboard private void hintKbTwo () {InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE); if (imm. isActive () & getCurrentFocus ()! = Null) {if (getCurrentFocus (). getWindowToken ()! = Null) {imm. hideSoftInputFromWindow (getCurrentFocus (). getWindowToken (), InputMethodManager. HIDE_NOT_ALWAYS );}}}

When you need to click an event to close the keyboard, you only need to call the method.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.