在文本字段中编写时,我需要我的文本字段向上移动,以便在弹出键盘时使文本字段可见。
libgdx是否有一种方法,可以返回true表示键盘可见,false表示键盘隐藏?
在文本字段中编写时,我需要我的文本字段向上移动,以便在弹出键盘时使文本字段可见。
libgdx是否有一种方法,可以返回true表示键盘可见,false表示键盘隐藏?
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...");
}
});
祝你好运!
我知道我在回答一个老帖子,但是我正在搜索答案,但无论如何都找不到。现在我已经自己创建了一个解决方案。以下是如何在Android上以优雅的方式完成此操作:)
我正在创建一个ApplicationBundle来捆绑接口以添加特定于平台的内容。如果您想利用RoboVM,您也可以在iOS上进行此操作。
我的解决方案:
在核心项目中创建一个SizeChangeListener接口:
public interface SizeChangeListener {
public void onSizeChange(float width, float height);
}
public interface View {
public void onSizeChange(float width, float height);
public void addListener(SizeChangeListener sizeChangeListener);
public float getWidth();
public float getHeight();
}
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;
}
}
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);
}
}
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);
}
});
}
private float getKeyboardHeight() {
return stage.getHeight() - view.getHeight();
}
view.getHeight()
以匹配舞台或相机尺寸。@Willempie您是否对iOS有可行的解决方案? - hamhamrootView = this.getWindow().getDecorView().getRootView();
这一行引起的,有什么办法可以避免这种情况吗? - Russ Wheeler尝试
Gdx.input.isPeripheralAvailable(Input.Peripheral.OnscreenKeyboard);
Gdx.input.setOnscreenKeyboardVisible(boolean visible);
也可以使用这种方法(像这样,您可以定义键盘何时可见,何时不可见)。