我如何在安卓系统中获取虚拟键盘的高度?这是否可能?
我尝试从主窗口中获取它,但是它给出了整个应用程序的完整高度。我想获取键盘的高度。
我如何在安卓系统中获取虚拟键盘的高度?这是否可能?
我尝试从主窗口中获取它,但是它给出了整个应用程序的完整高度。我想获取键盘的高度。
您无法获取键盘高度,但是您可以获取视图的高度,这才是您真正想要的 - 并且您将在 onLayout 调用中向当前视图提供此数据。
你可以使用这个示例代码。虽然不太优美,但它能正常工作。
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();
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
}
}
<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>
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 是软键盘的高度
}
});
如果你的应用不需要 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));
}
});
试试这个
KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null);
int height = (keyboardView.getKeyboard()).getHeight();
Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show();
keyboardView.getKeyboard()
返回null
。 - Dirk