Intercept the return key before the IME in Android

Source: Internet
Author: User

I use the scene: when the Input method display press the return key, if there is popupwindow first hide Popupwidow.

There are several steps to achieve this:

One, custom edittext:

public class Inputmethodedittext extends edittext{
Private Popujar mpopup;//Custom Popupwindow control
Public Popujar Getmpopup () {
return mpopup;
}
public void Setmpopup (Popujar mpopup) {
This.mpopup = Mpopup;
}
Public Inputmethodedittext (Context context) {
Super (context);
}
Public Inputmethodedittext (context context, AttributeSet Attrs) {
Super (context, attrs);
}
Public Inputmethodedittext (context context, AttributeSet attrs,
int Defstyle) {
Super (context, attrs, Defstyle);
}


@Override
public boolean Dispatchkeyeventpreime (KeyEvent event) {

if (event.getkeycode () = = Keyevent.keycode_back && Mpopup! = null && mpopup.isshow ()) {

This first intercepts the return key before the input method and hides my Popupwindow control
Mpopup.dismiss ();
return true;
}
Return Super.dispatchkeyeventpreime (event);
}

}

Second, the main control code: Messageinput = (inputmethodedittext) Findviewbyid (r.id.chat_content);
Messageinput.setmpopup (MPOPU);

The following code binds the input method to the current custom edittext.
Mimm = (Inputmethodmanager)
Messageinput.getcontext (). Getsystemservice (Context.input_method_service);

After the above two steps can be implemented in the input method before intercepting the Android back key, small remember backup.

Intercept the return key before the IME in Android

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.