Android監聽軟鍵盤收起與彈出
在專案中有時候會需要監聽軟鍵盤的彈出與收起,並沒有找到官方的API,所以根據網上的思路,自定義View來實現監聽.
並不複雜直接上程式碼吧:
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
public class MeasuredLayout extends LinearLayout {
private int largestHeight;
private OnKeyboardHideListener onKeyboardHideListener;
private int heightPrevious;
private int heightNow;
private View mChildOfContent;
private int usableHeightPrevious;
public MeasuredLayout(Context context, View view){
super(context);
addView(view);
}
public MeasuredLayout(Context context, AttributeSet attrs, int layoutId) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(layoutId, this);
mChildOfContent=getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// 鍵盤彈出
if (onKeyboardHideListener != null) {
onKeyboardHideListener.onKeyboardHide(false);
}
} else {
// 鍵盤收起
if (onKeyboardHideListener != null) {
onKeyboardHideListener.onKeyboardHide(true);
}
}
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
public interface OnKeyboardHideListener {
void onKeyboardHide(boolean hide);
}
public void setOnKeyboardHideListener(OnKeyboardHideListener onKeyboardHideListener) {
this.onKeyboardHideListener = onKeyboardHideListener;
}
}
使用的時候很簡單:
只需要
public View getContentView() {
MeasuredLayout measuredLayout = new MeasuredLayout(this, null, R.layout.activity_apply_cash);
measuredLayout.setOnKeyboardHideListener(this);
return measuredLayout;
}
將該方法返回的View塞給Activity的 setContentView();方法.
並在使用監聽的Activity實現MeasuredLayout.OnKeyboardHideListener 介面並重寫方法
@Override
public void onKeyboardHide(boolean hide) {
isKeyboardHide=hide;
if(hide){
//這裡我們就可以拿到軟鍵盤是否隱藏了
}
}
``
相關文章
- Android開發之監聽軟鍵盤狀態(彈出收回)Android
- iOS textField鍵盤彈出/收起 自動上下移iOS
- h5監聽手機鍵盤彈起H5
- 監聽鍵盤事件事件
- Android 監聽鍵盤狀態變化,並獲取鍵盤高度Android
- android實現底部彈出框與軟鍵盤衝突(全面屏虛擬鍵適配)Android
- python pynput監聽鍵盤Python
- flutter中監聽鍵盤Flutter
- Android中的EditText預設時不彈出軟鍵盤的方法Android
- Android Home鍵、鎖屏鍵監聽Android
- java鍵盤監聽之視窗監聽的實現Java
- android PopupWindow監聽返回鍵無效Android
- Android軟鍵盤彈出,覆蓋h5頁面輸入框問題AndroidH5
- Android軟鍵盤模式Android模式
- ios 最新系統bug與解決——微信公眾號中彈出鍵盤再收起時,原虛擬鍵盤位點選事件無效iOS事件
- vue的監聽鍵盤事件的快捷方法Vue事件
- Android 軟鍵盤踩坑記Android
- Flutter_Webview 鍵盤彈出問題FlutterWebView
- Spring事件釋出與監聽Spring事件
- H5頁面監聽Android物理返回鍵H5Android
- android手機的微信H5彈出的軟鍵盤擋住了文字框,如何解決?AndroidH5
- SpringBoot系列——事件釋出與監聽Spring Boot事件
- 移動端軟鍵盤彈出影響頁面佈局問題
- 安卓開發 點選空白處收起鍵盤安卓
- Android截圖監聽Android
- win10自動彈出觸控鍵盤Win10
- Spring事件釋出與監聽機制Spring事件
- win10平板鍵盤自動彈出怎麼關閉_win10平板鍵盤自動彈出處理方法Win10
- 一行程式碼解決UITableView鍵盤收起行程UIView
- 2018.03.31、Android-ObjectBox-監聽AndroidObject
- 鍵盤壞了怎麼用軟鍵盤 電腦怎麼調出桌面鍵盤
- 電腦軟鍵盤怎麼開啟 快速調出軟鍵盤的方法教程
- iOS鍵盤彈出時動畫時長失效問題iOS動畫
- 輸入框跟隨鍵盤彈出/隱藏移動
- 直播系統平臺搭建,控制鍵盤彈出收縮
- android開啟軟鍵盤部分內容上移Android
- 09-XSS鍵盤監聽、cookie竊取&檔案上傳繞過Cookie
- 短視訊平臺開發,點選輸入框時自動彈出軟鍵盤