国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android監(jiān)聽輸入法彈窗和關(guān)閉的實現(xiàn)方法

2019-12-12 04:38:27
字體:
供稿:網(wǎng)友

用過ios的都知道ios上輸入法關(guān)閉的同時會自動關(guān)閉輸入框,那么在android上如何實現(xiàn)監(jiān)聽輸入法彈出和關(guān)閉呢?本篇文章就為你提供了一種可靠的實現(xiàn)方式。

演示效果視頻地址

首先在AndroidManifest中配置

android:windowSoftInputMode="adjustResize"

這樣每次輸入法彈出和關(guān)閉都會重新計算高度實現(xiàn)把布局頂上去的效果

然后我們要自定義一個布局,監(jiān)聽布局大小變化

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, intdefStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}@Overrideprotected 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);}}

然后把上面的自定義布局作為跟布局放到你需要的Activity中去,然后在Activity中綁定監(jiān)聽事件

mRootLayout.setOnResizeListener(this);@Overridepublic void onResize(int w, int h, int oldw, int oldh) {//如果第一次初始化if (oldh == 0) {return;}//如果用戶橫豎屏轉(zhuǎn)換if (w != oldw) {return;}if (h < oldh) {//輸入法彈出} else if (h > oldh) {//輸入法關(guān)閉setCommentViewEnabled(false, false);}int distance = h - old;EventBus.getDefault().post(new InputMethodChangeEvent(distance,mCurrentImageId));}

這樣只要輸入法彈出和關(guān)閉就能自動實現(xiàn)監(jiān)聽,達到關(guān)閉輸入框的效果,這樣就和蘋果的體驗很一致。 到這里就介紹完了,如果有什么好的思路,也歡迎評論分享點贊! [Github demo地址](https://github.com/gupengcheng/CheckSoftInputDemo)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 贵南县| 株洲县| 揭阳市| 盐城市| 平南县| 海原县| 乡宁县| 安陆市| 成武县| 邓州市| 南昌县| 南靖县| 黄平县| 曲周县| 玉山县| 太仆寺旗| 满城县| 洛隆县| 揭东县| 栾城县| 尼木县| 新民市| 宁明县| 白河县| 安徽省| 雅江县| 宁城县| 调兵山市| 五寨县| 无棣县| 文登市| 开平市| 红原县| 从江县| 布拖县| 普陀区| 沙湾县| 那坡县| 津南区| 南丹县|