您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Android中怎么監聽鍵盤狀態,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
ViewTreeObserver
A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change…
Android框架提供了一個ViewTreeObserver類,它是一個View視圖樹的觀察者類。ViewTreeObserver類中定義了一系列的公共接口(public interface)。當一個View attach到一個窗口上時就會創建一個ViewTreeObserver對象,這樣當一個View的視圖樹發生改變時,就會調用該對象的某個方法,將事件通知給每個注冊的監聽者。
OnGlobalLayoutListener是ViewTreeObserver中定義的眾多接口中的一個,它用來監聽一個視圖樹中全局布局的改變或者視圖樹中的某個視圖的可視狀態的改變。當軟鍵盤由隱藏變為顯示,或由顯示變為隱藏時,都會調用當前布局中所有存在的View中的ViewTreeObserver對象的dispatchOnGlobalLayout()方法,此方法中會遍歷所有已注冊的OnGlobalLayoutListener,執行相應的回調方法,將全局布局改變的消息通知給每個注冊的監聽者。
view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
getWindowVisibleDisplayFrame
Retrieve the overall visible display size in which the window this view is attached to has been positioned in.
getWindowVisibleDisplayFrame()會返回窗口的可見區域高度,通過和屏幕高度相減,就可以得到軟鍵盤的高度了。
完整示例代碼
package com.cari.cari.promo.diskon.util;import android.content.Context;import android.graphics.Rect;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.View;import android.view.ViewTreeObserver;import java.util.LinkedList;import java.util.List;public class SoftKeyboardStateWatcher implements ViewTreeObserver.OnGlobalLayoutListener { public interface SoftKeyboardStateListener { void onSoftKeyboardOpened(int keyboardHeightInPx); void onSoftKeyboardClosed(); } private final List<SoftKeyboardStateListener> listeners = new LinkedList<>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; private Context mContext; //使用時用這個構造方法 public SoftKeyboardStateWatcher(View activityRootView, Context context) { this(activityRootView, false); this.mContext = context; } private SoftKeyboardStateWatcher(View activityRootView) { this(activityRootView, false); } private SoftKeyboardStateWatcher(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > dpToPx(mContext, 200)) { isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < dpToPx(mContext, 200)) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } /** * Default value is zero {@code 0}. * * @return last saved keyboard height in px */ public int getLastSoftKeyboardHeightInPx() { return lastSoftKeyboardHeightInPx; } public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.add(listener); } public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.remove(listener); } /** * @param keyboardHeightInPx 可能是包含狀態欄的高度和底部虛擬按鍵的高度 */ private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardOpened(keyboardHeightInPx); } } } private void notifyOnSoftKeyboardClosed() { for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardClosed(); } } } private static float dpToPx(Context context, float valueInDp) { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics); }}
可以看到, 我建了一個自己的一個Listener , 通過這個listener實現我們想要的監聽 , 然后在這里處理一些邏輯問題.
主要代碼還是在onGlobalLayout中:
首先通過activityRootView.getWindowVisibleDisplayFrame(r)
檢索此視圖所附加的窗口所在的整個可見顯示大小 ,然后減去,已顯示的視圖的高度 ,(r.bottom - r.top)就是顯示的view的下坐標和上坐標,差即為高度.
至此,我們得到了剩余的高度 . 這個高度可能就是鍵盤高度了, 為什么說可能呢?因為還么有考慮到頂部的狀態欄和底部的虛擬導航欄. 當然也可能不是鍵盤.
然后我們根據這個高度和之前已知的鍵盤狀態來判斷是否為鍵盤.并回調給監聽者.
使用
ScrollView scrollView = findViewById(R.id.ugc_scrollview); final SoftKeyboardStateWatcher watcher = new SoftKeyboardStateWatcher(scrollView, this); watcher.addSoftKeyboardStateListener( new SoftKeyboardStateWatcher.SoftKeyboardStateListener() { @Override public void onSoftKeyboardOpened(int keyboardHeightInPx) { ConstraintLayout.LayoutParams layoutParamsVideo = (ConstraintLayout.LayoutParams) mError1000tv.getLayoutParams(); layoutParamsVideo.setMargins(0, 0, 0, keyboardHeightInPx - ScreenUtils.getStatusHeight(UGCEditActivity.this) - ScreenUtils.getBottomStatusHeight(UGCEditActivity.this)); } @Override public void onSoftKeyboardClosed() { mError1000tv.setVisibility(View.GONE); } } );
Scrollview是整個頁面的根布局, 我通過監聽它來實現對整個布局的監聽.
mError1000tv就是我一開始提到的要緊貼鍵盤頂部顯示的一個textview了.
我通過LayoutParams給它設置邊距 , 只設置了底部邊距 , 值為返回的"鍵盤高度"- 頂部狀態欄高度-虛擬導航欄的高度. 得到真實的鍵盤高度.
在onSoftKeyboardOpened和onSoftKeyboardClosed這兩個回調中, 處理自己的邏輯就好了.
然后放上我這邊屏幕工具類ScreenUtils的代碼, 需要的可以復制下來
ScreenUtils
package com.cari.promo.diskon.util;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Rect;import android.util.DisplayMetrics;import android.view.Display;import android.view.View;import android.view.WindowManager;import java.lang.reflect.Method;public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 獲得屏幕高度 * * @param context * @return */ public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 獲得屏幕寬度 * * @param context * @return */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /** * 獲得狀態欄的高度 * * @param context * @return */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } /** * 獲取當前屏幕截圖,包含狀態欄 * * @param activity * @return */ public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; } /** * 獲取當前屏幕截圖,不包含狀態欄 * * @param activity * @return */ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; } /** * 獲取 虛擬按鍵的高度 * * @param context 上下文 * @return 虛擬鍵高度 */ public static int getBottomStatusHeight(Context context) { int totalHeight = getAbsoluteHeight(context); int contentHeight = getScreenHeight(context); return totalHeight - contentHeight; } /** * 獲取屏幕原始尺寸高度,包括虛擬功能鍵高度 * * @param context 上下文 * @return The absolute height of the available display size in pixels. */ private static int getAbsoluteHeight(Context context) { int absoluteHeight = 0; WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = null; if (windowManager != null) { display = windowManager.getDefaultDisplay(); } DisplayMetrics displayMetrics = new DisplayMetrics(); @SuppressWarnings("rawtypes") Class c; try { c = Class.forName("android.view.Display"); @SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); method.invoke(display, displayMetrics); absoluteHeight = displayMetrics.heightPixels; } catch (Exception e) { e.printStackTrace(); } return absoluteHeight; }}
上述就是小編為大家分享的Android中怎么監聽鍵盤狀態了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。