我的应用程序旨在收集数据。但只有在键盘可见时才能收集数据。 仅在用户正在输入时才收集数据是不够的,所以我肯定是需要知道键盘是否可见。有没有方法检测软键盘是否在Android中打开?
我知道,类似的问题被 (How to check visibility of software keyboard in Android?)之前发布, 但upvotes严重号码的最后答案是,并 我想了很多事情发生了Android,因为然后。
那么,我可以检测到键盘是否打开/可见?
我的应用程序旨在收集数据。但只有在键盘可见时才能收集数据。 仅在用户正在输入时才收集数据是不够的,所以我肯定是需要知道键盘是否可见。有没有方法检测软键盘是否在Android中打开?
我知道,类似的问题被 (How to check visibility of software keyboard in Android?)之前发布, 但upvotes严重号码的最后答案是,并 我想了很多事情发生了Android,因为然后。
那么,我可以检测到键盘是否打开/可见?
目前我们的Androidñ现在还没有直接的方式,如果键盘被打开或不检测。只有像检查屏幕尺寸的解决方案才能解决问题。然而,这并不是全面的,有时会给出虚假的信号,例如在屏幕旋转或进入Android N的多窗口模式。
正如你可能听说过的那样,没有直接的办法。但是,通过检查屏幕尺寸已经改变,通常可以发现这一点通过:
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
if (yOld > yNew) {
//Do Stuff Here
}
}
希望我帮助:d
在创建一个类
package com.dubaipolice.app.utils;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import java.util.LinkedList;
import java.util.List;
/**
* Created by dev101 on 1/13/15.
*/
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
float LIMIT = 100;
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 SoftKeyboardStateHelper(Context context, View activityRootView) {
this(activityRootView, false);
Resources r = context.getResources();
LIMIT = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics());
}
public SoftKeyboardStateHelper(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 > LIMIT) { // if more than 100 pixels, its probably a keyboard...
isSoftKeyboardOpened = true;
notifyOnSoftKeyboardOpened(heightDiff);
} else if (isSoftKeyboardOpened && heightDiff < LIMIT) {
isSoftKeyboardOpened = false;
notifyOnSoftKeyboardClosed();
}
}
public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
}
public boolean isSoftKeyboardOpened() {
return isSoftKeyboardOpened;
}
/**
* Default value is zero (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();
}
}
}
}
然后你活动的onCreate,添加以下行
final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(context, findViewById(R.id.parent));
softKeyboardStateHelper.addSoftKeyboardStateListener(softKeyboardStateListener);
其中R.id.parent是您的活动的ID父布局和softKeyboardStateListener定义如下
SoftKeyboardStateHelper.SoftKeyboardStateListener softKeyboardStateListener = new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
}
@Override
public void onSoftKeyboardClosed() {
}
};
完美的作品!非常感谢:) – error1337
这个工程。谢谢! –
我以前遇到过同样的问题。经过大量的试验和错误,并遵循其他人的建议,我想出了对我来说很好的实施方案。这里是link。
正如我所提到的,最后一个“有用的”答案是4年前的,并且从那以后很多已经改变了。我正在寻找一个最新的回答这个问题 – keinabel