在我的libgdx-project (适用于android)中,我使用的是TextField。当TextField接收到焦点时,将出现软键盘。一切都还好。当用户按下智能手机的BACK-Button时,键盘就会消失。我怎样才能捕捉到那个事件呢?有没有人对此场景有任何想法或经验?
附言:我设置了Gdx.input.setCatchBackKey(true),它实际上只是阻止我的应用程序退出。因此,当用户在键盘可见时点击BACK-Button时,它不会触发。
Best,Starcracker
发布于 2015-11-06 02:43:57
这里有一个似乎对我有用的变通方法:
首先,创建以下方法并从应用程序的onCreate()方法中调用它:
private static final int KEYBOARD_HEIGHT_THRESHOLD = 150;
private static final int CHECK_KEYBOARD_DELAY = 200; //ms
private void addKeyboardListener() {
final View rootView = graphics.getView();
final Rect layoutChangeRect = new Rect();
rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(CHECK_KEYBOARD_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
rootView.getWindowVisibleDisplayFrame(layoutChangeRect);
if (Math.abs(rootView.getHeight() - layoutChangeRect.height()) < KEYBOARD_HEIGHT_THRESHOLD) {
//keyboard closed
} else {
//keyboard opened
}
}
}).start();
}
});我为CHECK_KEYBOARD_DELAY和KEYBOARD_HEIGHT_THRESHOLD使用了适合我的任意值。这些值似乎是必需的,因为键盘是动画的。
希望这能有所帮助。
发布于 2015-05-27 03:39:47
您可以创建一个实现InputProcessor接口的类,并捕获back键被触摸的事件。如下所示(基于wiki中的示例):
public class MyInputProcessor implements InputProcessor {
// ... Rest of the InputProcessor methods
@Override
public boolean keyDown(int keycode) {
if(keycode == Keys.BACK){
// This will be executed when back button is pressed
return true; // Means this event has been handled
}
return false;
}
// ...
}如果您只需要捕获back键事件,那么更简洁的方法是扩展InputAdapter,它基本上允许您只覆盖所需的方法,而不是实现整个InputProcessor接口:
public class MyInputProcessor extends InputAdapter {
@Override
public boolean keyDown(int keycode) {
if(keycode == Keys.BACK){
// This will be executed when back button is pressed
return true; // Means this event has been handled
}
return false;
}别忘了设置输入处理器:
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);https://stackoverflow.com/questions/30442215
复制相似问题