Tôi biết tôi đang trả lời một chuỗi cũ, nhưng tôi đã googling để tìm câu trả lời cho câu hỏi này nhưng không thể tìm thấy câu trả lời ở bất cứ đâu. Bây giờ tôi đã tự tạo ra một giải pháp. Đây là cách để làm điều đó trên Android một cách thanh lịch :) Tôi đang tạo một ApplicationBundle để kết nối các giao diện để thêm các nền tảng cụ thể. Bạn cũng có thể làm điều này trên iOS nếu bạn muốn sử dụng RoboVM.
Giải pháp của tôi:
tạo một giao diện SizeChangeListener trong dự án cốt lõi:
public interface SizeChangeListener {
public void onSizeChange(float width, float height);
}
tạo Xem giao diện trong dự án cốt lõi:
public interface View {
public void onSizeChange(float width, float height);
public void addListener(SizeChangeListener sizeChangeListener);
public float getWidth();
public float getHeight();
}
tạo AndroidView thực hiện Xem giao diện :
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;
}
}
tạo ApplicationBundle trong dự án cốt lõi
public class ApplicationBundle {
private final View view;
public ApplicationBundle(View view) {
this.view = view;
}
public View getView() {
return view;
}
}
Hãy nhập khẩu cần thiết từ các dự án cốt lõi. Trong AndroidLauncher trong dự án Android thêm như sau:
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);
}
}
trong MyApp chính của bạn trong dự án cốt lõi trong phương pháp tạo() thêm một thi SizeChangeListener để xem bạn đã nhận được từ các nhà xây dựng.
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);
}
});
}
Có lẽ tạo ra một chút phương pháp bàn phím heigt như tôi đã làm:
private float getKeyboardHeight() {
return stage.getHeight() - view.getHeight();
}
thể trùng lặp của [Detect nếu bàn phím mềm xuất hiện trên màn hình] (http://stackoverflow.com/questions/4745988/ phát hiện-nếu-mềm-bàn phím-là-có thể nhìn thấy trên màn hình) –
Điều này không có gì để làm với libgdx, bạn muốn nghe cho một sự kiện Android bản địa.Câu hỏi này đã được hỏi và trả lời –
Kinda, nhưng nếu nó không được cung cấp bởi LibGDX, nỗ lực thực hiện để nghe sự kiện này là lớn hơn nhiều;). – dom