如何在Android中获得虚拟键盘的高度?有可能吗?
我试图从主窗口获取它,但它给了我应用程序的全高。但我想获得键盘高度。
答案 0 :(得分:4)
您无法获得键盘高度,但您可以获得View的高度,这正是您真正想要的 - 并且您将把这些数据提供给onLayout调用到当前视图中。
答案 1 :(得分:1)
您可以使用此示例代码。它是肮脏的解决方案,但它的工作原理
Thread t = new Thread(){
public void run() {
int y = mainScreenView.getHeight()-2;
int x = 10;
int counter = 0;
int height = y;
while (true){
final MotionEvent m = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,
x,
y,
INTERNAL_POINTER_META_STATE);
final MotionEvent m1 = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,
x,
y,
INTERNAL_POINTER_META_STATE);
boolean pointer_on_softkeyboard = false;
try {
getSingletonInstrumentation().sendPointerSync(m);
getSingletonInstrumentation().sendPointerSync(m1);
} catch (SecurityException e) {
pointer_on_softkeyboard = true;
}
if (!pointer_on_softkeyboard){
if (y == height){
if (counter++ < 100){
Thread.yield();
continue;
}
} else if (y > 0){
softkeyboard_height = mainScreenView.getHeight() - y;
}
break;
}
y--;
}
if (softkeyboard_height > 0 ){
// it is calculated and saved in softkeyboard_height
} else {
calculated_keyboard_height = false;
}
}
};
t.start();
答案 2 :(得分:1)
这个解决方案也是hacky但解决问题(至少对我来说)。
android:windowSoftInputMode="adjustResize"
标记。现在主要故事在onGlobalLayout()
。我计算了临时视图的y轴和根视图的高度之间的差异
final View view = findViewById(R.id.base); view.getViewTreeObserver()。addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
int rootViewHeight = view.getRootView().getHeight();
View tv = findViewById(R.id.temp_view);
int location[] = new int[2];
tv.getLocationOnScreen(location);
int height = (int) (location[1] + tv.getMeasuredHeight());
deff = rootViewHeight - height;
// deff is the height of soft keyboard
}
});
答案 3 :(得分:1)
如果您不想在应用中使用android:windowSoftInputMode="adjustResize"
。
你可以尝试这样的事情:
any_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = main_layout.getHeight();
Log.w("Foo", String.format("layout height: %d", height));
Rect r = new Rect();
main_layout.getWindowVisibleDisplayFrame(r);
int visible = r.bottom - r.top;
Log.w("Foo", String.format("visible height: %d", visible));
Log.w("Foo", String.format("keyboard height: %d", height - visible));
}
});
答案 4 :(得分:1)
让我们假设您的布局的rootView是RelativeLayout。 您可以创建一个类CustomRelativeLayout来扩展RelativeLayout和 覆盖其中的onSizeChanged方法。 因此,当软键盘打开时,RelativeLayout的高度将发生变化,并且 更改将在onSizeChanged方法内通知。
public class CustomRelativeLayout extends RelativeLayout {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int softKeyboardHeight = oldh - h; // Assuming softKeyboard Opened up
// oldh is the height when the layout was covering the whole screen
// h is the new height of the layout when the soft keyboard opened up
if(softKeyboardHeight > oldh * 0.15) {
Log.i("Here", Integer.toString(softKeyboardHeight));
// Keyboard has popped up
} else {
// Not the keyboard
}
}
在清单文件中进行这些更改,以使“活动”以“调整大小”模式打开,而不是“平移和扫描”模式。在“调整大小”模式下,打开键盘时,布局将能够调整大小。 要了解有关全景扫描和调整大小的更多信息,请访问 https://developer.android.com/training/keyboard-input/visibility
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
答案 5 :(得分:0)
试试这个
KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null);
int height = (keyboardView.getKeyboard()).getHeight();
Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show();