我须要作一个很是简单的事情-查找是否显示了软件键盘。 在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); } }