如何发送指针事件的Android [英] How to send out pointer event in Android
问题描述
我想检测Android的虚拟键盘高度。
I'm trying to detect the virtual keyboard height in Android.
我发现了一个类似的话题:获取虚拟键盘的Android中高度
I found a similar topic: Get the height of virtual keyboard in Android
看来笔者发现一种方法来检测高度:
It seems the author found a way to detect the height:
我找到一种方式来获得它。之后,我要求打开虚拟键盘,我
送我产生指针事件。他们的Y坐标从开始
设备的高度和降低。
I found a way to get it. After I request to open virtual keyboard, I send pointer event that I generate. their y coordinate starts from height of device and decreases.
我不明白该怎么做。
推荐答案
我会使用在你发布的链接提供的code:
I'll be using the code provided at the link that you posted:
// Declare Variables
int softkeyboard_height = 0;
boolean calculated_keyboard_height;
Instrumentation instrumentation;
// Initialize instrumentation sometime before starting the thread
instrumentation = new Instrumentation();
mainScreenView
是基本视图,您的活动的看法。 M
(ACTION_DOWN)和 M1
(ACTION_UP)是使用派遣触摸事件仪表# sendPointerSync(MotionEvent)
。其中的逻辑是,一个MotionEvent分派到其中所显示的键盘会导致以下 SecurityException异常
:
mainScreenView
is your base view, your activity's view. m
(ACTION_DOWN) and m1
(ACTION_UP) are touch events that are dispatched using Instrumentation#sendPointerSync(MotionEvent)
. The logic is that a MotionEvent dispatched to where the keyboard is being displayed will cause the following SecurityException
:
java.lang.SecurityException异常:注射到另一个应用程序需要
INJECT_EVENTS许可
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
于是,我们开始在屏幕的底部,使我们一路(按递减是
)的循环的每次迭代。对于某些迭代次数,我们将得到一个SecurityException(我们会赶上):这将意味着MotionEvent是发生在键盘上。当下是
变得足够小(当其正上方的键盘),我们将跳出循环,使用计算键盘的高度:
So, we start at the bottom of the screen and make our way up (by decrementing y
) on every iteration of the loop. For certain number of iterations, we will get a SecurityException (which we'll catch): this would imply that the MotionEvent is happening over the keyboard. The moment y
gets small enough (when its just above the keyboard), we'll break out of the loop and calculate the keyboard's height using:
softkeyboard_height = mainScreenView.getHeight() - y;
code:
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,
1);
final MotionEvent m1 = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,
x,
y,
1);
boolean pointer_on_softkeyboard = false;
try {
instrumentation.sendPointerSync(m);
instrumentation.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;
Log.i("", "Soft Keyboard's height is: " + softkeyboard_height);
}
break;
}
y--;
}
if (softkeyboard_height > 0 ){
// it is calculated and saved in softkeyboard_height
} else {
calculated_keyboard_height = false;
}
}
};
t.start();
仪表#sendPointerSync(MotionEvent)
:
派遣一个指针的事件。在接收方某一点后完成
已经从它的事件处理返回,虽然它可能没有
完全完成从事件反应 - 例如,如果它
需要更新其结果显示,它仍可能是在
这样做的过程。
Dispatch a pointer event. Finished at some point after the recipient has returned from its event processing, though it may not have completely finished reacting from the event -- for example, if it needs to update its display as a result, it may still be in the process of doing that.
这篇关于如何发送指针事件的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!