Android monitoring Input Method window and the implementation of the closure _android

Source: Internet
Author: User
Tags closure

iOS has been used to know that the input method on iOS will automatically turn off the input box, then on Android how to monitor the Input method pop-up and shutdown it? This article provides you with a reliable way to achieve this.

Demo Effect Video Address

First, configure in Androidmanifest

Android:windowsoftinputmode= "Adjustresize"

This way, every time the input method pops up and closes, the height is recalculated to achieve the effect of putting the layout on top

Then we're going to customize a layout that listens for layout size changes

 public class Checksoftinputlayout extends Framelayout {private Onresizelistener Monresi
Zelistener; Public Checksoftinputlayout {Super (context);} public Checksoftinputlayout AttributeSet attrs) {Super (context, attires);} public Checksoftinputlayout (context context, AttributeSet attrs, int def STYLEATTR) {Super (context, attrs, defstyleattr); @TargetApi (in) public checksoftinputlayout AttributeSet attrs, int defstyleattr, int defstyleres) {Super (context, Attrs, defstyleattr, defstyleres); @Override Pro tected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, old); if (Monresizelistener
!= null) {monresizelistener.onresize (W, H, OLDW, old);} public void Setonresizelistener (Onresizelistener listener) {This.monresizelistener = listener;} public Interface Onresi 
Zelistener {void onResize (int w, int h, int oldw, int old);} 

Then put the custom layout of the above into the activity you need, and then bind the listener event

Mrootlayout.setonresizelistener (this);
@Override public
void onResize (int w, int h, int oldw, int oldh) {
//If first initialized if
(OLDH = = 0) {
return;
   
    
//If the user screens convert if
(w!= oldw) {return
;
}
if (H < OLDH) {
//IME popup
} else if (H > Oldh) {
//IME close
setcommentviewenabled (false, false); 
    }
int distance = H-old;
Eventbus.getdefault (). Post (new Inputmethodchangeevent (Distance,mcurrentimageid));

   

This way, as long as the input method pop-up and shutdown can automatically monitor, to turn off the input box effect, so as the Apple experience is very consistent. This is the end of the introduction, if there is any good ideas, but also welcome comments to share the point of praise! [Github Demo address] (Https://github.com/gupengcheng/CheckSoftInputDemo)

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.