Android Monitor virtual keyboard hide and show events

Source: Internet
Author: User

Just started listening in the onconfigurationchanged, the result found that this method will be called when the configuration change is changed, such as the change of the config file, such as screen switch, Android reload the configuration file. Keyboard shadowing does not trigger the method.

Later, the following method is used to solve the keyboard hidden monitoring event.

The outermost layout of the activity

Finalview Activityrootview = Findviewbyid (r.id.activityroot);

Set a listener on the layout to listen for changes to its layouts
Activityrootview.getviewtreeobserver (). Addongloballayoutlistener (Newongloballayoutlistener () {

@Override
Publicvoid Ongloballayout () {

Compare the activity root layout with the current layout size

int heightdiff = Activityrootview.getrootview (). GetHeight ()-activityrootview.getheight ();
In fact, this heightdiff replaced by DP more reliable

if (Heightdiff >100) {

When the size exceeds 100, the virtual keyboard event is typically displayed

}else{

When the size is less than 100, the virtual keyboard or virtual keyboard is hidden

}
}

});

1, method One (if the input method is displayed on the window, then hide, or vice versa)

    1. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Imm.togglesoftinput (0, inputmethodmanager.hide_not_always);

2, Method Two (view is to accept the soft keyboard input views, show_forced means force display)

    1. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Imm.showsoftinput (view,inputmethodmanager.show_forced); Display keyboard
    1. Imm.hidesoftinputfromwindow (View.getwindowtoken (), 0); //Force hide keyboard

4 getting the Input method open state

    1. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Boolean isopen=imm.isactive (); //isopen returns True to indicate that the input method is open

Android Monitor virtual keyboard hide and show events

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.