我須要作一個很是簡單的事情-查找是否顯示了軟件鍵盤。 在Android中能夠嗎? java
我沒有作差別編碼,而是作了相似的事情,由於我在應用程序中有菜單選項。 android
final View root= findViewById(R.id.myrootview); root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = root.getRootView().getHeight() - root.getHeight(); Rect rectgle= new Rect(); Window window= getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rectgle); int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); if(heightDiff <= contentViewTop){ //Soft KeyBoard Hidden }else{ //Soft KeyBoard Shown } } });
就計算機而言,它一直存在,可是這個問題仍然使人難以置信! 併發
所以,我採起了以上答案,並對其進行了組合和完善... app
public interface OnKeyboardVisibilityListener { void onVisibilityChanged(boolean visible); } public final void setKeyboardListener(final OnKeyboardVisibilityListener listener) { final View activityRootView = ((ViewGroup) getActivity().findViewById(android.R.id.content)).getChildAt(0); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { private boolean wasOpened; private final int DefaultKeyboardDP = 100; // From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0); private final Rect r = new Rect(); @Override public void onGlobalLayout() { // Convert the dp to pixels. int estimatedKeyboardHeight = (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics()); // Conclude whether the keyboard is shown or not. activityRootView.getWindowVisibleDisplayFrame(r); int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); boolean isShown = heightDiff >= estimatedKeyboardHeight; if (isShown == wasOpened) { Log.d("Keyboard state", "Ignoring global layout change..."); return; } wasOpened = isShown; listener.onVisibilityChanged(isShown); } }); }
爲我工做:) ide
注意:若是您發現DefaultKeyboardDP不適合您的設備使用該值,並發表評論讓你們知道該值是什麼...最終,咱們將得到適合全部設備的正確值! ui
有關更多詳細信息,請查看Cyborg上的實現 this
抱歉,個人答案很晚,可是我建立了一個小助手類來處理打開/關閉事件,並通知監聽者和其餘有用的東西,也許有人會以爲有用: 編碼
import android.graphics.Rect; 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<SoftKeyboardStateListener>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; public SoftKeyboardStateWatcher(View activityRootView) { this(activityRootView, false); } public SoftKeyboardStateWatcher(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); //r will be populated with the coordinates of your view that area still visible. activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < 100) { 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); } 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(); } } } }
用法示例: spa
final SoftKeyboardStateWatcher softKeyboardStateWatcher = new SoftKeyboardStateWatcher(findViewById(R.id.activity_main_layout); // Add listener softKeyboardStateWatcher.addSoftKeyboardStateListener(...); // then just handle callbacks
這個想法是,若是您須要隱藏鍵盤並同時檢查軟輸入狀態,請使用如下解決方案: code
public boolean hideSoftInput() { InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); return imm.hideSoftInputFromWindow(mViewPager.getWindowToken(), 0); }
若是在隱藏以前顯示了鍵盤,則此方法返回true。
您可使用活動的decorView觀察軟鍵盤的隱藏。
public final class SoftKeyboardUtil { public static final String TAG = "SoftKeyboardUtil"; public static void observeSoftKeyBoard(Activity activity , final OnSoftKeyBoardHideListener listener){ final View decorView = activity.getWindow().getDecorView(); decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); decorView.getWindowVisibleDisplayFrame(rect); int displayHight = rect.bottom - rect.top; int hight = decorView.getHeight(); boolean hide = (double)displayHight / hight > 0.8 ; if(Log.isLoggable(TAG, Log.DEBUG)){ Log.d(TAG ,"DecorView display hight = "+displayHight); Log.d(TAG ,"DecorView hight = "+ hight); Log.d(TAG, "softkeyboard visible = " + !hide); } listener.onSoftKeyBoardVisible(!hide); } }); } public interface OnSoftKeyBoardHideListener{ void onSoftKeyBoardVisible(boolean visible); } }