The solution of Android edittext by soft keyboard masking _android

Source: Internet
Author: User
Tags diff

This two-day Android app has added a transparency bar effect, finding that the keyboard bounces off the edittext at the bottom of the screen, doesn't look like the size of the window, and scrolls ScrollView to display the EditText on top of the keyboard. Have encountered similar problems before, so after the solution simply write all about edittext and the keyboard between the problems and solutions are recorded for later inspection.

First, before 5.0, if EditText set one of the gravity= "Center|right" and set singleline= "true" at the same time, it will cause the edittext on the bottom of the screen to click the pop-up keyboard continuously, From the second meeting has been blocking the edittext.

5.0+ will not have the problem, the solution is also simple, in the edittext outer envelope scrollview, and set the keyboard mode for the Adjustresize mode can be (both indispensable).

Second, according to the above description of the solution, has been no problem, until recently added a transparent bar effect, keyboard mode as if directly invalid, each click on the bottom edittext, the keyboard from the bottom of the pop-up, the window size will not adjust, the bottom of the edittext will also be covered. The final discovery is when using the GetWindow (). Getdecorview (). Setsystemuivisibility method Set System_ui_flag_layout_fullscreen or System_ui_flag_ Layout_hide_navigation, or when the Window.addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) is set, Causes the edittext to be obscured by the keyboard.

Because the transparent bar effect is supported from 4.4, it causes the bug to start from 4.4, of course, only consider 5.0+ transparent bar effect, you can set the status bar color to achieve transparent bar effect, I here because the project needs to start from 4.4 support, so must solve this problem.

Under normal circumstances, the system UI takes up some of the app's space, such as the status bar, the keyboard, the navigation bar, and so on, which means that our app UI doesn't appear under the system UI, but from the test results it is covered by the system UI in order to occupy the status bar space or full screen.

 Workaround:

Monitor the interface container layout changes, when the change, by checking the window visible area height, judge whether the keyboard is bouncing, if bouncing, then modify the container bottom padding, that is, manually realize adjustresize effect, to the keyboard to leave a display space, This scrollview also automatically resizes to display edittext on top of the keyboard.

public class Keyboardpatch {private activity activity;
 Private View Decorview;
 Private View Contentview; 
  /** * Constructor * @param act needs to solve the bug's activity * @param Contentview interface container, activity is generally r.id.content, or may be fragment container, according to individual needs to pass
  * */Public Keyboardpatch (Activity Act, View contentview) {this.activity = act;
  This.decorview = Act.getwindow (). Getdecorview ();
 This.contentview = Contentview; /** * Monitor Layout change */public void enable () {Activity.getwindow (). Setsoftinputmode (WINDOWMANAGER.LAYOUTPARAMS.S Oft_input_state_hidden |
  WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); if (Build.VERSION.SDK_INT >=) {decorview.getviewtreeobserver (). Addongloballayoutlistener (
  Ongloballayoutlistener); }/** * Cancel listener */public void Disable () {Activity.getwindow (). Setsoftinputmode (WindowManager.LayoutParams.SOF T_input_state_hidden |
  WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); if (Build.VERSION.SDK_INT >=) {Decorview.getviewtreeObserver (). Removeongloballayoutlistener (Ongloballayoutlistener); } private Viewtreeobserver.ongloballayoutlistener Ongloballayoutlistener = new
   Viewtreeobserver.ongloballayoutlistener () {@Override public void Ongloballayout () {Rect r = new Rect ();
   Decorview.getwindowvisibledisplayframe (R);
   int height = Decorview.getcontext (). Getresources (). Getdisplaymetrics (). Heightpixels;
   int diff = Height-r.bottom;
    if (diff!= 0) {if (Contentview.getpaddingbottom ()!= diff) {contentview.setpadding (0, 0, 0, diff);
    } else {if (Contentview.getpaddingbottom ()!= 0) {contentview.setpadding (0, 0, 0, 0);
}
   }
  }
 }; }

The above is a small set to introduce the Android EditText by the soft keyboard covering the treatment method, 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!

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.