libgdx如何检测键盘的存在

时间:2012-03-07 16:04:59

标签: android libgdx

在文本字段中书写时,我需要将文本字段向上移动,以便在键盘弹出时让文本字段可见。

libgdx是否有某种方法在键盘可见时返回true而在键盘关闭时返回false?

3 个答案:

答案 0 :(得分:13)

以下代码将检测您何时按下文本字段,阻止其显示键盘,然后打开使用键盘上下移动的本机对话框。它将从原生对话框中获取输入,最后将其放回textField:

    textField.setOnscreenKeyboard(new TextField.OnscreenKeyboard() {
        @Override
        public void show(boolean visible) {
            //Gdx.input.setOnscreenKeyboardVisible(true);
            Gdx.input.getTextInput(new Input.TextInputListener() {
                @Override
                public void input(String text) {
                    textField.setText(text);
                }

                @Override
                public void canceled() {
                    System.out.println("Cancelled.");
                }
            }, "Title", "Default text...");
        }
    });

祝你好运!

答案 1 :(得分:6)

我知道我回答了一个旧帖子,但是我在Google上搜索这个问题的答案,但无法在任何地方找到答案。现在我自己创建了一个解决方案。以下是如何在Android上以优雅的方式做到这一点:) 我正在创建一个ApplicationBundle来捆绑接口以添加特定于平台的东西。如果您想使用RoboVM,也可以在iOS上执行此操作。

我的解决方案:

在核心项目中创建一个SizeChangeListener接口:

public interface SizeChangeListener {
    public void onSizeChange(float width, float height);
}

在核心项目中创建一个View界面:

public interface View {
    public void onSizeChange(float width, float height);
    public void addListener(SizeChangeListener sizeChangeListener);
    public float getWidth();
    public float getHeight();
}

创建一个实现View界面的AndroidView:

public class AndroidView implements View {

    private ArrayList<SizeChangeListener> listeners = new ArrayList<SizeChangeListener>();
    private float width, height;
    public AndroidView(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void addListener(SizeChangeListener listener) {
        listeners.add(listener);
    }

    public void onSizeChange(float width, float height) {
        this.width = width;
        this.height = height;
        for(SizeChangeListener listener : listeners)
            listener.onSizeChange(width, height);
    }

    public float getWidth() {
        return width;
    }

    public float getHeight() {
        return height;
    }

}

在核心项目中创建一个ApplicationBundle

public class ApplicationBundle {

    private final View view;

    public ApplicationBundle(View view) {
        this.view = view;
    }

    public View getView() {
        return view;
    }
}

从核心项目中进行必要的导入。在Android项目的AndroidLauncher中添加以下内容:

public class AndroidLauncher extends AndroidApplication {

    private View rootView;
    private AndroidView androidView;
    private int width, height;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        rootView = this.getWindow().getDecorView().getRootView();
        Rect rect = new Rect();
        rootView.getWindowVisibleDisplayFrame(rect);
        width = rect.width();
        height = rect.height();
        androidView = new AndroidView(width, height);

        rootView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

            @Override
            public void onLayoutChange(View v, int left, int top, int right,
                    int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

                Rect rect = new Rect();
                rootView.getWindowVisibleDisplayFrame(rect);

                if(!(width == rect.width() && height == rect.height())) {
                    width = rect.width();
                    height = rect.height();
                    androidView.onSizeChange(width, height);
                }
            }

        }); 

        initialize(new DigMeApp(new ApplicationBundle(androidView)), config);
    }
}

在create()方法核心项目的主MyApp中,将SizeChangeListener实现添加到您从构造函数中获取的视图中。

public class MyApp extends Game { // or ApplicationAdapter
    private View view;
    private Stage stage;
    // your own variables

    public MyApp(ApplicationBundle applicationBundle) {
        view = applicationBundle.getView();
    }

    @Override
    public void create () {
        stage = new Stage();
        // add some textfields
    final TextField tf1 = new TextField("", skin);
    final TextField tf2 = new TextField("", skin);

    tf1.setWidth((float)view.getWidth() * 0.6f);
    tf2.setWidth((float)view.getWidth() * 0.6f);
    tf1.setHeight((float)view.getHeight() * 0.05f);
    tf2.setHeight((float)view.getHeight() * 0.05f);
        view.addListener(new SizeChangeListener() {         
            @Override
            public void onSizeChange(float width, float height) {
                Gdx.app.log("INFO", "Visible area: " + width + "   " + height);
                Gdx.app.log("INFO", "Stage area: " + stage.getWidth() + "   " + stage.getHeight());
                float keyboardHeight = getKeyboardHeight();

// MOVE THEM OUT OF THE WAY :)

                tf1.addAction(Actions.moveTo(width / 2 - tf1.getWidth() / 2.0f, keyboardHeight + (6 * (height / 8)), 1, Interpolation.sineOut));
                tf2.addAction(Actions.moveTo(width / 2 - tf2.getWidth() / 2.0f, keyboardHeight + (7 * (height / 8)), 1, Interpolation.sineOut));


//              Gdx.gl20.
//              tf.setPosition(width / 2 - (tf.getWidth() / 2.0f), 0);
            }
        });
}

也许像我一样创建一个小键盘heigt方法:

private float getKeyboardHeight() {
        return stage.getHeight() - view.getHeight();
    }

答案 2 :(得分:1)

尝试

Gdx.input.isPeripheralAvailable(Input.Peripheral.OnscreenKeyboard);

我只是在文档中看了这个,不知道它是否真的有效。但是

Gdx.input.setOnscreenKeyboardVisible(boolean visible);
也可以使用

方法(就像你在键盘可见和不可见时一样定义)。