Home>

Anyone who has used iOS knows that the input box will be closed automatically when the input method is closed on iOS.So how to monitor the input method popup and close on android?Next, I will share a reliable implementation method through this article.

Anyone who has used iOS knows that the input box will be closed automatically when the input method is closed on iOS.So how to monitor the input method popup and close on android?This article provides you with a reliable implementation.

Demo effect video address

First configure in androidmanifest

android:windowsoftinputmode="adjustresize"

In this way, each time the input method pops up and closes, it will recalculate the height to achieve the effect of topting the layout

Then we have to customize a layout,Listen for layout size changes

public class checksoftinputlayout extends framelayout {
private onresizelistener monresizelistener;
public checksoftinputlayout (context context) {
super (context);
}
public checksoftinputlayout (context context, attributeset attrs) {
super (context, attires);
}
public checksoftinputlayout (context context, attributeset attrs, int
defstyleattr) {
super (context, attrs, defstyleattr);
}
@targetapi (21)
public checksoftinputlayout (context context, attributeset attrs, int
defstyleattr, int defstyleres) {
super (context, attrs, defstyleattr, defstyleres);
}
@override
protected 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 onresizelistener {
void onresize (int w, int h, int oldw, int old);
}
}

Then put the above custom layout as the layout in the activity you need, and then bind the listener event in the activity

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 switches between portrait and landscape
if (w!=oldw) {
return;
}
if (h<oldh) {
//Input method pops up
} else if (h>oldh) {
//input method is closed
setcommentviewenabled (false, false);
}
int distance=h-old;
eventbus.getdefault (). post (new inputmethodchangeevent (distance, mcurrentimageid));
}
  • Previous C # Lambda Knowledge Review
  • Next jquery to achieve blinds effect