如何检查软件键盘的知名度的Android? [英] How to check visibility of software keyboard in Android?
问题描述
我需要做一个很简单的事情 - 看看软键盘显示。这是可能在Android中?
I need to do a very simple thing - find out if the software keyboard is shown. Is this possible in Android?
推荐答案
新的答案 补充2012年1月25日的
由于写下面的答案,有人避让我的<一个存在href="http://developer.android.com/reference/android/view/ViewTreeObserver.html">ViewTreeObserver和朋友,原料药已经潜伏在SDK版本以来1。
Since writing the below answer, someone clued me in to the existence of ViewTreeObserver and friends, APIs which have been lurking in the SDK since version 1.
,而不需要自定义布局类型,一个更简单的解决办法就是给你活动的根查看已知的ID,说'@ + ID / activityRoot,勾GlobalLayoutListener到ViewTreeObserver,并从那里计算之间的大小差异您的活动的视图根和窗口大小:
Rather than requiring a custom Layout type, a much simpler solution is to give your activity's root view a known ID, say '@+id/activityRoot', hook a GlobalLayoutListener into the ViewTreeObserver, and from there calculate the size diff between your activity's view root and the window size:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});
轻松!
注意:
您的应用程序必须设置该标志在Android清单机器人:windowSoftInputMode =adjustResize
,否则上述解决方案将无法正常工作
Note:
Your application must set this flag in Android Manifest android:windowSoftInputMode="adjustResize"
otherwise above solution will not work.
原来的答案
是的,它是可能的,但它远远难度比它应该是。
Yes it's possible, but it's far harder than it ought to be.
如果我需要关心的时候,键盘的出现和消失(这是相当频繁),那么我要做的就是定制我的顶层布局类为一个将覆盖 onMeasure()
。基本逻辑是如果布局发现自己填充显著小于窗口的总面积,则软键盘被大概示出。
If I need to care about when the keyboard appears and disappears (which is quite often) then what I do is customize my top-level layout class into one which overrides onMeasure()
. The basic logic is that if the layout finds itself filling significantly less than the total area of the window, then a soft keyboard is probably showing.
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/*
* LinearLayoutThatDetectsSoftKeyboard - a variant of LinearLayout that can detect when
* the soft keyboard is shown and hidden (something Android can't tell you, weirdly).
*/
public class LinearLayoutThatDetectsSoftKeyboard extends LinearLayout {
public LinearLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface Listener {
public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
Activity activity = (Activity)getContext();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
int diff = (screenHeight - statusBarHeight) - height;
if (listener != null) {
listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
然后在你的Activity类...
Then in your Activity class...
public class MyActivity extends Activity implements LinearLayoutThatDetectsSoftKeyboard.Listener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
LinearLayoutThatDetectsSoftKeyboard mainLayout = (LinearLayoutThatDetectsSoftKeyboard)findViewById(R.id.main);
mainLayout.setListener(this);
...
}
@Override
public void onSoftKeyboardShown(boolean isShowing) {
// do whatever you need to do here
}
...
}
这篇关于如何检查软件键盘的知名度的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!