android 鍵盤彈起/收起監聽
阿新 • • 發佈:2019-02-07
Android soft (virtual) keyboard listener
If you use Evernote android app you might have noticed, that when, on a login screen, soft keyboard gets shown (to type password or username) the layout doesn't just scale, or scrolls. It changes (for example Evernotes logo is hidden). Unfortunately, android api doesn't provide you with the specific tools to listen for a moment, when soft keyboard gets shown, or hidden. But there is a way (dirty hack) to assume that the keyboards state was changed. We can guess it by listening measure changes in layout. To do it, first, we need to make our layout to resize and scale instaed of scroll. Add android :windowSoftInputMode="adjustResize" parameter
to your activity in manifest.xml. This way, when soft keyboard is called, content will be resized instead of scrolled.
Next step is to actually listen for this resizing. And again, there is a catch. There is no such thing as OnMeasureChangedListener, so the only way is to extend the container layout and do it inside. As an example I extended LinearLayout, and this is what
I've got:
If you use Evernote android app you might have noticed, that when, on a login screen, soft keyboard gets shown (to type password or username) the layout doesn't just scale, or scrolls. It changes (for example Evernotes logo is hidden). Unfortunately, android api doesn't provide you with the specific tools to listen for a moment, when soft keyboard gets shown, or hidden. But there is a way (dirty hack) to assume that the keyboards state was changed. We can guess it by listening measure changes in layout. To do it, first, we need to make our layout to resize and scale instaed of scroll. Add android