真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android中怎么監(jiān)聽鍵盤狀態(tài)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android中怎么監(jiān)聽鍵盤狀態(tài),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10年的常州網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整常州建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“常州網(wǎng)站設(shè)計(jì)”,“常州網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

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框架提供了一個(gè)ViewTreeObserver類,它是一個(gè)View視圖樹的觀察者類。ViewTreeObserver類中定義了一系列的公共接口(public interface)。當(dāng)一個(gè)View attach到一個(gè)窗口上時(shí)就會創(chuàng)建一個(gè)ViewTreeObserver對象,這樣當(dāng)一個(gè)View的視圖樹發(fā)生改變時(shí),就會調(diào)用該對象的某個(gè)方法,將事件通知給每個(gè)注冊的監(jiān)聽者。

OnGlobalLayoutListener是ViewTreeObserver中定義的眾多接口中的一個(gè),它用來監(jiān)聽一個(gè)視圖樹中全局布局的改變或者視圖樹中的某個(gè)視圖的可視狀態(tài)的改變。當(dāng)軟鍵盤由隱藏變?yōu)轱@示,或由顯示變?yōu)殡[藏時(shí),都會調(diào)用當(dāng)前布局中所有存在的View中的ViewTreeObserver對象的dispatchOnGlobalLayout()方法,此方法中會遍歷所有已注冊的OnGlobalLayoutListener,執(zhí)行相應(yīng)的回調(diào)方法,將全局布局改變的消息通知給每個(gè)注冊的監(jiān)聽者。

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()會返回窗口的可見區(qū)域高度,通過和屏幕高度相減,就可以得到軟鍵盤的高度了。

完整示例代碼

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 listeners = new LinkedList<>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; private Context mContext; //使用時(shí)用這個(gè)構(gòu)造方法 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 可能是包含狀態(tài)欄的高度和底部虛擬按鍵的高度  */ 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); }}

可以看到, 我建了一個(gè)自己的一個(gè)Listener , 通過這個(gè)listener實(shí)現(xiàn)我們想要的監(jiān)聽 , 然后在這里處理一些邏輯問題.

主要代碼還是在onGlobalLayout中:

首先通過activityRootView.getWindowVisibleDisplayFrame(r)檢索此視圖所附加的窗口所在的整個(gè)可見顯示大小 ,然后減去,已顯示的視圖的高度 ,(r.bottom - r.top)就是顯示的view的下坐標(biāo)和上坐標(biāo),差即為高度.

至此,我們得到了剩余的高度 . 這個(gè)高度可能就是鍵盤高度了, 為什么說可能呢?因?yàn)檫€么有考慮到頂部的狀態(tài)欄和底部的虛擬導(dǎo)航欄. 當(dāng)然也可能不是鍵盤.

然后我們根據(jù)這個(gè)高度和之前已知的鍵盤狀態(tài)來判斷是否為鍵盤.并回調(diào)給監(jiān)聽者.

使用

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是整個(gè)頁面的根布局, 我通過監(jiān)聽它來實(shí)現(xiàn)對整個(gè)布局的監(jiān)聽.

mError1000tv就是我一開始提到的要緊貼鍵盤頂部顯示的一個(gè)textview了.

我通過LayoutParams給它設(shè)置邊距 , 只設(shè)置了底部邊距 , 值為返回的"鍵盤高度"- 頂部狀態(tài)欄高度-虛擬導(dǎo)航欄的高度. 得到真實(shí)的鍵盤高度.

在onSoftKeyboardOpened和onSoftKeyboardClosed這兩個(gè)回調(diào)中, 處理自己的邏輯就好了.

然后放上我這邊屏幕工具類ScreenUtils的代碼, 需要的可以復(fù)制下來

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; } /**  * 獲得狀態(tài)欄的高度  *  * @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; } /**  * 獲取當(dāng)前屏幕截圖,包含狀態(tài)欄  *  * @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; } /**  * 獲取當(dāng)前屏幕截圖,不包含狀態(tài)欄  *  * @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中怎么監(jiān)聽鍵盤狀態(tài)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


當(dāng)前標(biāo)題:Android中怎么監(jiān)聽鍵盤狀態(tài)
URL鏈接:http://weahome.cn/article/ipjsdj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部