Android Alternative method listens for pop-up events on the soft keyboard

Source: Internet
Author: User

Http://www.cnblogs.com/csonezp/p/5065624.html

Recent projects have encountered a problem, a interface is Fragment+recyclerview,b interface with a edittext, and will automatically get focus pop-up soft keyboard. From the A interface into the B interface, and then the end of the B interface back to a interface, you will leave a screen on the A and soft keyboard size of the same gray area, the specific reason for a long time is not clear. But the problem is always to solve, I fragment on a a half-day, always useless. Then think about the B interface tossing and tossing, close the activity when the first creases keyboard, soft keyboard closed and then finish.

Inputmethodmanager there is no soft keyboard to collect the monitoring events, Internet search also did not find. Now that you can't find a specific listener event, you should start with something else. Think of the soft keyboard pop-up window size will change, just b logic does not design window size changes, so rewrite b the bottom of the layout of the Onsizechange event, with the window size to replace the soft keyboard popup/collection event.

First define a listener:

/***/Publicinterface  inputwindowlistener    {void Show ();     void hidden ();}

This listener is used to inform the activity.

The bottom layout of the B interface is relativelayout, so we're going to rewrite a relativelayout:

/*** Created by Csonezp on 15-12-21. * Use Onsizechange to simulate pop-up events for soft keyboards*/ Public classImmlistenerrelativelayoutextendsRelativelayout {PrivateInputwindowlistener Listener;  PublicImmlistenerrelativelayout (Context context) {Super(context); }     PublicImmlistenerrelativelayout (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicImmlistenerrelativelayout (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Overrideprotected voidOnsizechanged (intWintHintOLDW,intOLDH) {        Super. Onsizechanged (W, H, OLDW, OLDH); if(Oldh >h) {L.D ("Input window Show");        Listener.show (); } else{L.D ("Input window Hidden");        Listener.hidden (); }    }     Public voidSetlistener (Inputwindowlistener listener) { This. Listener =Listener; }}

Don't spit out the name of the slot, casually up.

Focus on the Onsizechanged method, if the soft keyboard from the collection into a pop-up, that OLDH is full-screen height, h is full-screen height minus the height of the soft keyboard, if the soft keyboard from pop-up into a collection, then H is full screen height, Oldh conversely.

So we can determine the state of the soft keyboard.

In the activity layout, replace the original relativelayout with a custom layout, and then

Mainlayout.setlistener (new  Inputwindowlistener () {            @Override            publicvoid  Show () {                            }            @Override            publicvoid  hidden () {                           }        });

You can do what you want to do.

Android Alternative method listens for pop-up events on the soft 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.