<Android> Monitor Soft keyboard to open the Closed event (soft keyboard comes with a close button)

Source: Internet
Author: User

Recently in the company development cocos2dx Android Input Box control, encountered the soft keyboard event monitoring, usually the soft keyboard to pick up the way roughly 3 kinds:

1. Click the return button in the lower right corner of the soft keyboard (the system is closed)

2. Press the Back button when input box focus (System is closed)

3. Click outside of the soft keyboard and input box (spontaneous collection)

4. Click the button that comes with the soft keyboard (soft keyboard to pick up)


The first three kinds of events can be monitored, the way is relatively simple

1. Click the return button in the lower right corner of the soft keyboard

Setting the input box to listen

Edittext.setoneditoractionlistener (New Oneditoractionlistener () {
@Override
Public boolean oneditoraction (final TextView V, final int ActionId, final keyevent event) {
Snap to the return button in the lower right corner

Add a throw Up event code
return false;
}
});


2. Press the Back button when entering the box focus

Add a button monitor to the input box

Edittext.setonkeylistener (New Onkeylistener () {
@Override
public boolean OnKey (View v, int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
if (listener! = null) {
You can add a throw up event code
}
return true;
}
return false;
}
});


3. Click outside the soft keyboard and input box

Add touch monitoring to the parent container of the input box

@Override
public boolean ontouchevent (Motionevent event) {

if (Indexofchild (EditText) >-1) {

You can add a throw up event code

}

Return Super.ontouchevent (event);

}


4. Click the button that comes with the soft keyboard (soft keyboard to pick up)

Problem card here, after the data search, or can find the soft keyboard to close the button event monitoring method, finally found in the StackOverflow website from the layout height changes to determine the soft keyboard open and the collection of events.

Import Android.graphics.rect;import Android.view.view;import Android.view.viewtreeobserver;import Java.util.linkedlist;import Java.util.list;public class Softkeyboardstatehelper implements Viewtreeobserver.ongloballayoutlistener {public interface Softkeyboardstatelistener {void onsoftkeyboardopened        (int keyboardheightinpx);    void onsoftkeyboardclosed ();    } private Final list<softkeyboardstatelistener> listeners = new linkedlist<softkeyboardstatelistener> ();    Private final View Activityrootview;    private int lastsoftkeyboardheightinpx;    Private Boolean issoftkeyboardopened;    Public Softkeyboardstatehelper (View Activityrootview) {This (Activityrootview, false);     } public Softkeyboardstatehelper (View Activityrootview, Boolean issoftkeyboardopened) {This.activityrootview        = Activityrootview;        this.issoftkeyboardopened = issoftkeyboardopened; Activityrootview.getviewtreeobserver (). AddongloballayoutlistenER (this);        } @Override public void Ongloballayout () {final rect r = new Rect ();        R'll is populated with the coordinates of your view, the area still visible.        Activityrootview.getwindowvisibledisplayframe (R);        Final int heightdiff = Activityrootview.getrootview (). GetHeight ()-(r.bottom-r.top);            if (!issoftkeyboardopened && heightdiff >) {//If more than-pixels, its probably a keyboard ...            Issoftkeyboardopened = true;        Notifyonsoftkeyboardopened (Heightdiff);            } else if (issoftkeyboardopened && Heightdiff <) {issoftkeyboardopened = false;        Notifyonsoftkeyboardclosed (); }} public void Setissoftkeyboardopened (Boolean issoftkeyboardopened) {this.issoftkeyboardopened = Issoftkey    boardopened;    } public boolean issoftkeyboardopened () {return issoftkeyboardopened; }/** * Default value is zero (0) * @return last saved KEyboard height in px */public int getlastsoftkeyboardheightinpx () {return lastsoftkeyboardheightinpx;    } public void Addsoftkeyboardstatelistener (Softkeyboardstatelistener listener) {Listeners.add (listener);    } public void Removesoftkeyboardstatelistener (Softkeyboardstatelistener listener) {listeners.remove (listener); } private void notifyonsoftkeyboardopened (int keyboardheightinpx) {this.lastsoftkeyboardheightinpx = Keyboar        DHEIGHTINPX; for (Softkeyboardstatelistener listener:listeners) {if (listener! = null) {Listener.onsoftke            Yboardopened (KEYBOARDHEIGHTINPX);  }}} private void Notifyonsoftkeyboardclosed () {for (Softkeyboardstatelistener listener:listeners)            {if (listener! = null) {listener.onsoftkeyboardclosed (); }        }    }}

Use of the code:

Final Softkeyboardstatehelper softkeyboardstatehelper = new Softkeyboardstatehelper (Findviewbyid (R.id.activity_main _layout); Softkeyboardstatehelper.addsoftkeyboardstatelistener (...); /Then just handle callbacks


Address Link: http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android

This code is selected from the.

This can be solved together with the above 4 cases, better listening to the soft keyboard open and close events, is currently in use.


<Android> Monitor Soft keyboard to open the Closed event (soft keyboard comes with a close button)

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.