Android Click on the screen blank to close the keyboard (manually open) _android

Source: Internet
Author: User
Tags gety

Many times, we use the application, there will be the input method soft keyboard pop-up problem, under normal circumstances, we will allow users to click the return key or next to the soft keyboard to hide. For a better experience, we can achieve when the user is finished using the soft keyboard. Click the blank area of the screen to realize the soft keyboard function of the input method. Here to introduce the implementation of the method.

1.//Hide Soft keyboard

In the Java file:

Inputmethodmanager m = (Inputmethodmanager) mcontext.getsystemservice (context.input_method_service);
  M. Hidesoftinputfromwindow (Xxxx.getwindowtoken (), 0);//For example EditView

Or is:

Write in manifest.xml activity: Android:windowSoftInputMode="stateHidden indicates that the activity hidden input method is opened.

2,//display soft keyboard, control ID can be Edittext,textview

 
 

3, does not automatically eject the keyboard:

The ideal way to achieve this:

public class Homeactivity extends activity {... @Override public boolean dispatchtouchevent (Motionevent ev) { if (ev.getaction () = = Motionevent.action_down) {//Get the View that is currently in focus, usually edittext (special case is the track request or the entity case will move the focus) View V = ge 
  Tcurrentfocus (); 
  if (Isshouldhideinput (v, Ev)) {hidesoftinput (V.getwindowtoken ()); 
 } return super.dispatchtouchevent (EV); 
 /** * To determine whether to hide the keyboard based on the EditText's coordinates and the user-clicked coordinates, because when the user clicks on the edittext there is no need to hide * * @param v * @param event * @return * * Private Boolean isshouldhideinput (View V, motionevent event) {if (v!= null && (v instanceof edittext)) {i 
  Nt[] 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 on EditText event to ignore it. 
  return false; 
  else {return true; }//If the focusNot EditText is ignored, this occurs after the view has just been drawn, the first focus is not on the EditView, and the user uses the trackball to select the other focus return false; 
  /** * One of several hidden software disk methods * * @param token/private void Hidesoftinput (IBinder token) {if (token!= null) { 
  Inputmethodmanager im = (inputmethodmanager) getsystemservice (Context.input_method_service); 
 Im.hidesoftinputfromwindow (token, inputmethodmanager.hide_not_always);  } 
 } 
 ...... 
}

The above is a small series to introduce Android click on the screen blank space to close the keyboard (manually open), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.