画面に表示されているキーボードのサイズを知る方法はありますか?
プログラミングにCocos2dxを使用していますが、Androidの部分またはCocosの部分の画面に表示されるキーボードの高さを知りたいのですが、問題ありません。
キーボードにgetHeight()メソッドがあることは知っていますが、新しいキーボードを作成したくないので、デフォルトのキーボードを使用します。
これでやった
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
parent.getWindowVisibleDisplayFrame(r);
int screenHeight = parent.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
Log.d("Keyboard Size", "Size: " + heightDifference);
}
});
ビューのサイズはキーボードでのみ変更されるため、これを使用できます。
Rect r = new Rect();
View rootview = this.getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);
この結果、アプリケーションが画面上で使用するスペースの量になります(アクティビティのサイズが変更されていない場合でも機能します)。明らかに残りの画面スペースはキーボードによって使用されます(表示されている場合)
ここでIDが見つかりました:https ://github.com/freshplanet/ANE-KeyboardSize/blob/master/android/src/com/freshplanet/ane/KeyboardSize/getKeyboardY.java
アクティビティがフルスクリーンでない場合は、以下のコードを使用してください。
content.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
if (keyBoardHeight <= 100) {
Rect r = new Rect();
content.getWindowVisibleDisplayFrame(r);
int screenHeight = content.getRootView()
.getHeight();
int heightDifference = screenHeight
- (r.bottom - r.top);
int resourceId = getResources()
.getIdentifier("status_bar_height",
"dimen", "android");
if (resourceId > 0) {
heightDifference -= getResources()
.getDimensionPixelSize(resourceId);
}
if (heightDifference > 100) {
keyBoardHeight = heightDifference;
}
Log.d("Keyboard Size", "Size: " + heightDifference);
}
// boolean visible = heightDiff > screenHeight / 3;
}
});
アクティビティのサイズが変更されていないときに仮想キーボードの高さを計算する場合(adjustPan)、次のサンプルを使用できます。
https://github.com/siebeprojects/samples-keyboardheight
ウィンドウとアクティビティのルートビューの間の高さの差を計算するために、非表示のウィンドウを使用します。
これは古い投稿ですが、選択したソリューションがすべてのデバイスで機能するとは限らないことに気付きました。不一致があるようだったので、これを実装しましたが、すべてをキャッチしているようです。
final int[] discrepancy = new int[1];
discrepancy[0] = 0;
// this gets the height of the keyboard
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
View rootview = activity.getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);
int screen_height = rootview.getRootView().getHeight();
int keyboard_height = screen_height - (r.bottom + r.top) - discrepancy[0];
if (discrepancy[0] == 0) {
discrepancy[0] = keyboard_height;
if (keyboard_height == 0) discrepancy[0] = 1;
}
int margin_bottom = keyboard_height + Helper.getDp(10, activity);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) carousel_container.getLayoutParams();
params.setMargins(0, 0, 0, margin_bottom);
//boolean visible = heightDiff > screenHeight / 3;
}
});
リスナーが最初に呼び出されたとき、キーボードなしで画面を測定します。不一致がある場合は、次回にそれを説明します。不一致がない場合は、不一致を1に設定して、0ではなくなったようにします。
cocos2d-xにはCCEditBoxがあります。
Extensions-> GUI-> CCEditBoxの中に、クラスCCEditBoxがあります。
美しさは、シーンのどこかをタップするキーボードを隠すことです。編集ボックスがシーンの低すぎる位置にある場合は、キーボードを自動的に上に移動します。
cocos2d-x v2.1.3を使用している場合は、次の場所に移動してサンプルプロジェクトに移動できます。
サンプル->cpp->TestCpp-> Classes->ExtensionTest->EditBoxTest。
これからはCCTextFieldの代わりに使用します。昨日出くわしたばかりです:)
2020年以降、最小SDKが21以上の場合、以下の関数でIMEの可視性と高さを確認できます。
fun isKeyboardVisible(attachedView: View): Boolean {
val insets = ViewCompat.getRootWindowInsets(attachedView)
return insets?.isVisible(WindowInsetsCompat.Type.ime()) ?: false
}
fun getKeyboardHeight(attachedView: View): Int {
val insets = ViewCompat.getRootWindowInsets(attachedView)
return insets?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
}
参照:キーボードのアニメーション化(パート1)。チェック用の新しいWindowInsetsAPI…| クリス・ベインズ Android開発者| 中くらい
何時間も検索した後、設定したい場合は解決策を見つけましたwindowSoftInput="adjustPan"
コードスニペットは次のとおりです。
final View root = findViewById(android.R.id.content);
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
Rect r = new Rect();
{
root.getWindowVisibleDisplayFrame(r);
}
@Override
public void onGlobalLayout() {
Rect r2 = new Rect();
root.getWindowVisibleDisplayFrame(r2);
int keyboardHeight = r.height() - r2.height();
if (keyboardHeight > 100) {
root.scrollTo(0, keyboardHeight);
}
else {
root.scrollTo(0, 0);
}
}
});
このコードでは、キーボードの高さを見つけた後、キーボードで覆われていないところまでビューをスクロールします。これが、キーボードの高さを見つける主な理由です。
ドキュメントによると:
void getWindowVisibleDisplayFrame(Rect outRect)
:このビューがアタッチされているウィンドウが配置されている、表示されている全体的な表示サイズを取得します。
Androidの表示画面のROOT_VIEWは、アクティビティのビューを表示するVISIBLEDISPLAYFRAMEを使用した単一の画面ビューとして視覚化できます。
この表示フレームは、ソフトキーボードが画面に表示または非表示になっているときに調整されます。
注:理解を深めるために、以下のリンクをクリックして2つの画像をご覧ください。
したがって、表示画面のルートビューは次のように視覚化できます。表示画面のルート ビュー
ソフトキーボードの開閉によるVISIBLEDISPLAYFRAMEの調整は、次のように視覚化できます 。VISIBLE_DISPLAY_SCREEN調整
ビジュアルディスプレイフレームのこの調整は、キーボードの高さを次のように確認するために非常によく使用できます。
(ソフトキーボードが開いているとき)
SOFT_KEYBOARD_HEIGHT = ROOT_VIEW_HEIGHT-(VISUAL_DISPLAY_FRAME_HEIGHT + EXTRA_SCREEN_HEIGHT)
上記を実現するためのコードは次のとおりです。
int mExtraScreenHeight=-1, mKeyboardHeight=-1;
boolean mKeyboardOpen;
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int rootViewHeight, visibleDisplayFrameHeight, fakeHeight;
/* (rootViewHeight - visibleDisplayFrameHeight) is not the real height of the keyboard
it is the fake height as it also consist of extra screen height
so FAKE_HEIGHT = KEYBOARD_HEIGHT + EXTRA_SCREEN_HEIGHT
To get keyboard height extra screen height must be removed from fake height
*/
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
rootViewHeight = rootView.getRootView().getHeight();
visibleDisplayFrameHeight = rect.height();
fakeHeight = rootViewHeight-visibleDisplayFrameHeight;
if (mExtraScreenHeight == -1){
mExtraScreenHeight=fakeHeight;
}
/* Suppose the soft keyboard is open then the VISIBLE_DISPLAY_FRAME is in reduced size
due to the space taken up by extra screen and the keyboard but when the soft keyboard closes
then KEYBOARD_HEIGHT=0 and thus FAKE_HEIGHT = EXTRA_SCREEN_HEIGHT
*/
else if (fakeHeight <= mExtraScreenHeight){
mExtraScreenHeight=fakeHeight;
mKeypadOpen=false;
}
else if (fakeHeight > mExtraScreenHeight){
mKeypadHeight=fakeHeight-mExtraScreenHeight;
mKeypadOpen=true;
}
}
});
注:onGlobalLayout()関数は、ソフトキーボードが開いたときのようにグローバルレイアウトが変更されたときにのみ呼び出されます。したがって、ソフトキーボードの高さを取得するには、ソフトキーボードを少なくとも1回開く必要があります。
それは私のために働いた;)
コメントできず申し訳ありませんが、2つまたは3つの回答が私の問題の解決に役立ち、AddOnGlobalLayoutListenerを使用して、キーボードが表示される前後の残りの高さを決定することに関連していました。
私が使用したソリューションは、Rudy_TMの回答に基づいていました。
しかし、私が見つけなければならなかったことの1つは、そのメソッドが機能するためには、どこかに次の行が必要であるということでした。
Window.SetSoftInputMode(SoftInput.AdjustResize);
SoftInput.AdjustNothing(またはそのようなもの)を使用する前は、機能しませんでした。今では完璧に動作します。答えてくれてありがとう!
完全な答えと私のために完璧に働いた:
Rect r = new Rect();
View rootview = this.getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);
int keyboardHeight = rootview.getHeight() - r.bottom;