For Android, click the blank area to hide the keyboard of the input method.

Source: Internet
Author: User
Tags gety

For Android, click the blank area to hide the keyboard of the input method.
Welcome to blog with Markdown Editor

Most of the time, when we use an application, the soft keyboard of the input method will pop up. Normally, we will enable the user to click back or hide the soft keyboard in the next step by default. For a better user experience, we can achieve when the user has used the keyboard. Click the blank area to hide the function. Effect:

Code Implementation

The code block syntax follows the standard markdown code, for example:

Package example.com. jinlin. myapplication; import android. content. context; import android. OS. bundle; import android. OS. IBinder; import android. support. v7.app. appCompatActivity; import android. view. motionEvent; import android. view. view; import android. view. inputmethod. inputMethodManager; import android. widget. editText;/*** Created by J! Nl! N on 15/5/21. */public abstract class BaseActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); iniView ();} public abstract void iniView (); @ Override public boolean dispatchTouchEvent (MotionEvent ev) {if (ev. getAction () = MotionEvent. ACTION_DOWN) {View v = getCurrentFocus (); if (isShouldHideKeyboard (v, ev) {hide Keyboard (v. getWindowToken ();} return super. dispatchTouchEvent (ev);}/*** determines whether to hide the keyboard based on the relative ratio of the EditText coordinates and the user-clicked coordinates, because when you click EditText, you cannot hide ** @ param v * @ param event * @ return */private boolean isShouldHideKeyboard (View v, MotionEvent event) {if (v! = Null & (v instanceof EditText) {int [] l = {0, 0}; v. getLocationInWindow (l); int left = l [0], top = l [1], bottom = top + v. getHeight (), right = left + v. getWidth (); if (event. getX ()> left & event. getX () <right & event. getY ()> top & event. getY () <bottom) {// click the EditText event and ignore it. Return false;} else {return true;} // ignore if the focus is not EditText. This occurs when the view has just been drawn and the first focus is not on EditText, and the user uses the trackball to select another focus. return false;}/*** get InputMethodManager, hide the soft keyboard * @ param token */private void hideKeyboard (IBinder token) {if (token! = Null) {InputMethodManager im = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE); im. hideSoftInputFromWindow (token, InputMethodManager. HIDE_NOT_ALWAYS );}}}

Of course, we still have a simpler method to implement this function. You only need to override the onTouchEvent method. The Code is as follows:

// Automatically hide the soft keyboard public boolean onTouchEvent (MotionEvent event) {if (null! = This. getCurrentFocus () {/*** click a blank location to hide the soft keyboard */InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService (INPUT_METHOD_SERVICE); return mInputMethodManager. hideSoftInputFromWindow (this. getCurrentFocus (). getWindowToken (), 0);} return super. onTouchEvent (event );}

You can use a BaseActivity to perform some common operations. All other activities inherit from this base Activity class. All interfaces can be clicked in a blank area to hide the keyboard.
 

Related Article

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.