问题
I have in my LibGDX android application a table, below some textfields (one row) and a button below (Submit). My question is, how to "slide" up the table, textfiels and the button when the textfield is focuses (click on it) and the soft keyboard is appearing? In other words, how to make the soft keyboard not to cover my other objects. This solution might be related with this question: How can i find the height of soft-keyboard in a LibGDX application
Many thanks.
回答1:
I found an workaround for my problem, it's working, but I don't think is too clean. So, my scenario was:
- a container table, containing a ScrollPane (another table), under this scrolling table two TextFields and under them a Submit button
- container table set to fill parent (full screen)
- the text fields and the button aligned to the bottom of the container table
The problem was that when the OnscreenKeybord was visible, it covered the bottom objects (text fields and button) and a part of the scrolling table.
My solution / workaround is:
keyboard = new OnscreenKeyboard() {
@Override
public void show(boolean visible) {
Gdx.input.setOnscreenKeyboardVisible(visible);
// Hmmmm...
container.invalidate();
if (visible) {
// TODO get OsK height somehow!!!
container.padBottom(310);
} else {
container.padBottom(0);
}
}
};
textFld1.setOnscreenKeyboard(keyboard);
textFld2.setOnscreenKeyboard(keyboard);
and on the InputListener of the Submit button (on touchDown) I have a cleanup function like:
textFld1.setText("");
textFld2.setText("");
stage.unfocus(textFld1);
stage.unfocus(textFld2);
keyboard.show(false);
In this way the container table will be padded up when the soft keyboard is displayed and it will be restored after the submit button is pressed; also the soft keyboard is hidden.
Now, there are two problems with this workaround:
I have not found a way to get the soft keyboard height; as you can see in the sample code I have chosen an arbitrary value for the up padding, so the tings to look good on my emulator
if hard button Back or ESC in emulator is pressed, the soft keyboard will hide, but the up-padding of the table will still be there; I didn't find how to determine if the soft keyboard is visible or not
If anyone finds another solution for my problem, please post it.
Thanks.
回答2:
i was totally wrestling with this problem too, but i think i found a really nice solution.
Solution in theory:
when the keyboard pops up, it makes the screen resize, thus calling libgdx's resize callback.
Advantages:
- no need to have logic having to do with keyboard visibility detection
- no need to get height of keyboard
- handle everythin in the resize callback of the libgdx's screen class
- simple to implement (i think)
Disadvantages
- app can no longer be fullscreen; the status bar shows and things
How to do it:
Create a
main_layout.xml
file, (....YourLibGdxThing\android\res\layout\main_layout.xml
) such that has the following XML:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/main_frame_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
modify your android launcher class (
....YourLibGdxThing\android\src\your\name\space\android\AndroidLauncher.java
) such that the onCreate looks like this:public class AndroidLauncher extends AndroidApplication { @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main_frame_layout); frameLayout.addView(initializeForView(new YourGame(),config)); } }
change
android:windowFullScreen
attribute in thestyle.xml
(....YourLibGdxThing\android\res\values\styles.xml
) to false; the XML should look like this instyle.xml
:<resources> <style name="GdxTheme" parent="android:Theme"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowFullscreen">false</item> <!-- IMPORTANT --> </style> </resources>
add the attribute
android:windowSoftInputMode="adjustResize"
to the android manifest, into the activity tag...my AndroidManifest (....YourLibGdxThing\android\AndroidManifest.xml
) looks like this:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.project.reverse.domain.thing" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/GdxTheme" > <activity android:name="my.project.reverse.domain.thing.AndroidLauncher" android:label="@string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize" <!-- IMPORTANT --> android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Thanks! I hope it helps!!!!!!!!!!
回答3:
Set android:windowSoftInputMode
to adjustPan
in your <activity>
block to get the result you want. See docs: https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
来源:https://stackoverflow.com/questions/21467006/how-to-push-up-the-screen-when-soft-keybord-is-displayed