我有一个活动使用“ adjustPan”作为其调整大小配置,并且我需要不使用“ adjustResize”来计算键盘高度,因为我需要将某些视图保持为全屏显示(这意味着它们应该留在原处,键盘应将其隐藏),然后在键盘上方放置一个视图。我们的应用程序有一个消息按钮,我通过单击按钮打开键盘。发生这种情况时,我使用OnGlobalLayoutListener并使用“ getWindowVisibleDisplayFrame”方法获取键盘的高度。这是一些代码:
private void message()
{
InputMethodManager methodManager =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (methodManager != null && !isKeyboardOpen)
{
methodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
if (bottomCoordinate == 0)
{
RelativeLayout layout = findViewById(getFullScreenContainerId());
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Rect r = new Rect();
layout.getWindowVisibleDisplayFrame(r);
bottomCoordinate = r.bottom - r.top;
translateMessageView(bottomCoordinate);
}
});
}
else
translateMessageView(bottomCoordinate);
isKeyboardOpen = true;
}
}
“ translateMessageView”基本上将视图的Y坐标设置为“ bottomCoordinate-view.getHeight()”。直到软键盘应用程序的自动更正部分可见之前,此方法都可以正常工作。显然,“ getWindowVisibleDisplayFrame”方法似乎未添加视图的自动更正部分,或者在软键盘的自动更正部分显示出来且定位的视图停留在其下方时,未调用“ onGlobalLayout”方法,从而使其处于半可见状态。我需要能够再次调整其位置,该怎么办?正确的方法是什么?任何建议都是有价值的,谢谢。
答案 0 :(得分:0)
(不是原始答案)
Rect r = new Rect();
查看rootview = this.getWindow()。getDecorView(); //这=活动
rootview.getWindowVisibleDisplayFrame(r);
其结果是应用程序在屏幕上使用的空间量(即使未调整活动大小也可以使用)。显然,剩余的屏幕空间将由键盘使用(如果可见)
您可以访问原始答案
答案 1 :(得分:0)
这是我在任何活动中检测键盘高度的方法,如果存在缺口/切出高度,这也可以满足要求。
KeyboardHeightProvider.java
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.DisplayCutout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import androidx.core.view.DisplayCutoutCompat;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class KeyboardHeightProvider extends PopupWindow implements OnApplyWindowInsetsListener {
private View decorView;
private LinearLayout popupView;
private ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener;
private Rect insets = new Rect(0, 0, 0, 0);
public KeyboardHeightProvider(Context context, WindowManager windowManager, View decorView, KeyboardHeightListener listener) {
super(context);
this.decorView = decorView;
popupView = new LinearLayout(context);
popupView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
globalLayoutListener = () -> {
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
Rect rect = new Rect();
popupView.getWindowVisibleDisplayFrame(rect);
int keyboardHeight = metrics.heightPixels - (rect.bottom - rect.top) - (insets.bottom - insets.top);
int resourceID = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceID > 0) {
keyboardHeight -= context.getResources().getDimensionPixelSize(resourceID);
}
if (keyboardHeight < 100) {
keyboardHeight = 0;
}
boolean screenLandscape = metrics.widthPixels > metrics.heightPixels;
if (listener != null) {
listener.onKeyboardHeightChanged(keyboardHeight, screenLandscape);
}
};
setContentView(popupView);
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
setWidth(0);
setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
setBackgroundDrawable(new ColorDrawable(0));
ViewCompat.setOnApplyWindowInsetsListener(popupView, this);
}
public void start() {
popupView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
decorView.post(() -> showAtLocation(decorView, Gravity.NO_GRAVITY, 0, 0));
}
@Override
public void dismiss() {
popupView.getViewTreeObserver().removeOnGlobalLayoutListener(globalLayoutListener);
super.dismiss();
}
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
DisplayCutoutCompat cutoutCompat = insets.getDisplayCutout();
if (cutoutCompat != null) {
this.insets.set(cutoutCompat.getSafeInsetLeft(), cutoutCompat.getSafeInsetTop(), cutoutCompat.getSafeInsetRight(), cutoutCompat.getSafeInsetBottom());
} else {
this.insets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
}
if (decorView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
if (rootWindowInsets != null) {
DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
if (displayCutout != null) {
this.insets.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
}
}
}
return insets;
}
public interface KeyboardHeightListener {
void onKeyboardHeightChanged(int height, boolean isLandscape);
}
}
关于您的活动:
android:windowSoftInputMode
可以是任何东西,我的是stateHidden|adjustNothing
KeyboardHeightProvider.KeyboardHeightListener
添加一个全局变量:
private KeyboardHeightProvider keyboardHeightProvider;
在onCreate
中添加以下行:
keyboardHeightProvider = new KeyboardHeightProvider(this, getWindowManager(), getWindow().getDecorView(), this);
在onResume
中添加以下行:
keyboardHeightProvider.start();
在onPause
中添加以下行:
keyboardHeightProvider.dismiss();
在onKeyboardHeightChanged
@Override
public void onKeyboardHeightChanged(int height, boolean isLandscape) {
//This will be called anytime the keyboard height has changed.
boolean keyboardOpen = height > 0;
//do what you want with Keyboard Height
}