获取Android中虚拟键盘的高度
如何在Android中获得虚拟键盘的高度?可能吗获取Android中虚拟键盘的高度,android,android-softkeyboard,Android,Android Softkeyboard,如何在Android中获得虚拟键盘的高度?可能吗 我试图从主窗口获取它,但它提供了应用程序的完整高度。但是我想要得到键盘高度。你不能得到键盘高度,但是你可以得到视图的高度,这是你真正想要的——你可以得到这些数据,并将其提供给当前视图中的onLayout调用。你可以使用这个示例代码。这是一个肮脏的解决方案,但它是有效的 Thread t = new Thread(){ public void run() { int y = mainScree
我试图从主窗口获取它,但它提供了应用程序的完整高度。但是我想要得到键盘高度。你不能得到键盘高度,但是你可以得到视图的高度,这是你真正想要的——你可以得到这些数据,并将其提供给当前视图中的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();
Thread t=新线程(){
公开募捐{
int y=mainScreenView.getHeight()-2;
int x=10;
int计数器=0;
整数高度=y;
while(true){
最终MotionEvent m=MotionEvent.Get(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION\u向下,
x,,
Y
内部\指针\元\状态);
最终MotionEvent m1=MotionEvent.Get(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION\u UP,
x,,
Y
内部\指针\元\状态);
软键盘上的布尔指针=false;
试一试{
getSingletonInstrumentation().sendPointerSync(m);
getSingletonInstrumentation().sendPointerSync(m1);
}捕获(安全异常e){
软键盘上的指针=true;
}
如果(!指针在软键盘上){
如果(y==高度){
如果(计数器+++<100){
螺纹屈服强度();
继续;
}
}如果(y>0),则为else{
软键盘_height=mainScreenView.getHeight()-y;
}
打破
}
y--;
}
如果(软键盘高度>0){
//计算并保存在软键盘上
}否则{
计算的键盘高度=假;
}
}
};
t、 start();
此解决方案也有问题,但解决了问题(至少对我来说)
android:windowSoftInputMode=“adjustResize”
标志onGlobalLayout()
中。在这里,我计算了临时视图的y轴和根视图的高度之间的差值
最终视图=findViewById(R.id.base);
view.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener()){
}))KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null);
int height = (keyboardView.getKeyboard()).getHeight();
Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show();
如果您不想在应用程序中使用android:WindowsOfInputMode=“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));
}
});
让我们假设布局的根视图是相对的。 您可以创建一个类CustomRelativeLayout,该类扩展RelativeLayout和 重写其中的onSizeChanged方法。 因此,当软键盘打开时,相对位置的高度将发生变化,并且 更改将在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
}
}
在清单文件中进行这些更改,以便活动以调整大小模式而不是平移和扫描模式打开。在调整大小模式下,当键盘打开时,布局将能够调整大小。
要了解有关平移扫描和调整大小的更多信息,请访问
软键盘并不是系统提供的唯一可以占用屏幕空间的装饰。你打算用这个尺寸做什么?我想在柔软的virtualkeyboard上面画一些图像。我开发2D应用程序,所以我不使用android UI库。例如,我想在软键盘的顶部绘制边框虚拟键盘是否会向上推动窗口?在这种情况下,您只需要在屏幕底部画一条线来绘制上边框(在激活软键盘之前设置为不可见),它只会在窗口未全屏时向上推窗口。我的视图显示设备的全高。所以,这些信息并没有给出虚拟键盘的高度。但是,我找到了一个办法。请求打开虚拟键盘后,我发送生成的指针事件。它们的y坐标从设备高度开始,然后减小。在我将这些事件发送到系统之后,当指针事件位于虚拟键盘上时,系统抛出异常。如果这些指针事件不在虚拟键盘上,则不存在异常。所以我可以得到虚拟键盘的高度。它实际上是针对带有导航栏的android机型的。内部指针元状态在哪里?metaState被引用到KeyEvent API:但找不到内部\指针\元\状态。内部\指针\元\状态只是标识此事件源的id。因此,您可以跟踪由您或设备生成的指针事件。您只需将一个整数值设置为INTERNAL\u POINTER\u META\u state什么是getSingletonInstrumentation()方法?看起来非常简单,只有两行代码。。。但是它对我不起作用。getKeyboard()返回的值对我来说也是一样。keyboardView.getKeyboard()返回的值可能不太准确,但是如果你不需要监听器,最好使用这个。getWindowVisibleDisplayFrame对我不起作用。它返回视图的高度
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>