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
- Android開發之監聽軟鍵盤狀態(彈出收回)Android
- 監聽Android軟體盤彈出及隱藏Android
- android開發中,監聽軟鍵盤的狀態(收起或者開啟)Android
- Android 監聽鍵盤彈起及隱藏Android
- iOS textField鍵盤彈出/收起 自動上下移iOS
- 阻止android軟鍵盤自動彈出Android
- h5監聽手機鍵盤彈起H5
- 監聽鍵盤事件事件
- 鍵盤監聽事件--向左事件
- javascript監聽鍵盤事件JavaScript事件
- Android 監聽鍵盤狀態變化,並獲取鍵盤高度Android
- android實現底部彈出框與軟鍵盤衝突(全面屏虛擬鍵適配)Android
- python pynput監聽鍵盤Python
- Android中的EditText預設時不彈出軟鍵盤的方法Android
- java鍵盤監聽之視窗監聽的實現Java
- java全域性滑鼠鍵盤監聽Java
- ios 最新系統bug與解決——微信公眾號中彈出鍵盤再收起時,原虛擬鍵盤位點選事件無效iOS事件
- 關於Android中軟鍵盤顯示隱藏的監聽判斷總結Android
- Android Home鍵監聽Android
- Android Home鍵、鎖屏鍵監聽Android
- Android軟鍵盤彈出,覆蓋h5頁面輸入框問題AndroidH5
- vue的監聽鍵盤事件的快捷方法Vue事件
- Java中的鍵盤監聽事件KeyListenerJava事件
- Android軟鍵盤模式Android模式
- android PopupWindow監聽返回鍵無效Android
- Flutter_Webview 鍵盤彈出問題FlutterWebView
- home鍵監聽(Android應用切換到後臺監聽)Android
- android 軟鍵盤 輸入法 彈出引起的各種不適終極解決方案Android
- android設定軟鍵盤搜尋鍵以及監聽搜尋鍵點選時發生兩次事件的問題解決Android事件
- 安卓開發 點選空白處收起鍵盤安卓
- Android 監聽home鍵的動作Android
- Android軟鍵盤與EditText近乎完美的互動Android
- js監聽鍵盤事件程式碼例項例項JS事件
- 移動端軟鍵盤彈出影響頁面佈局問題
- flutter中監聽鍵盤Flutter
- flash呼叫攝像頭彈出設定框監聽
- Android 軟鍵盤踩坑記Android