大佬教程收集整理的这篇文章主要介绍了如何检查Android中软件键盘的可见性?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
NEW ANSWER 添加于2012年1月25日
自从写下以下答案以来,有人让我了解到VIEwTreeObserver和朋友,自版本1以来就一直潜伏在SDK中的API。
不需要自定义Layout类型,一种更简单的解决方案是为活动的根视图提供一个已知的ID,例如@+ID/activityRoot,将GlobalLayoutListener挂接到VIEwTreeObserver中,然后从中计算活动的视图根与窗口大小之间的大小差:
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 > dptopx(this, 200)) { // if more than 200 dp, it's probably a keyboard...
// ... do something here
}
}
});
使用实用程序,例如:
public static float dptopx(Context context, float valueInDp) {
displayMetrics metrics = context.getResources().getdisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
简单!
注意: 您的应用程序必须在AndroID Manifest中设置此标志,androID:windowsoftinputMode=”adjustResize”否则以上解决方案将不起作用。
原始答案
是的,这是可能的,但它比应做的要难得多。
如果我需要关心键盘何时出现和消失(这是很常见的),那么我要做的就是将顶层布局类自定义为覆盖onMeasure()。基本逻辑是,如果布局发现自己填充的内容明显少于窗口的总面积,那么可能会显示软键盘。
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类中…
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中可以吗?
以上是大佬教程为你收集整理的如何检查Android中软件键盘的可见性?全部内容,希望文章能够帮你解决如何检查Android中软件键盘的可见性?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。