使用全屏模式时,检测软输入可见性和高度

时间:2013-04-10 10:35:24

标签: android android-layout

我已阅读此问题的多篇帖子,解决方法是使用OnGlobalLayoutListener。这里的问题是我的应用程序在清单中声明了以下内容:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

因此输入模式调整不起作用,我无法使用OnGlobalLayoutListener

如何获得软输入可见/消失的事件,我还需要获得输入的高度以调整我的当前布局bottomMargin

感谢。

3 个答案:

答案 0 :(得分:1)

我不知道你如何使用OnGlobalLayoutListener,但它在我的代码中运行良好:

        final View activityRootView = findViewById(R.id.container);
        final ViewTreeObserver observer = activityRootView
                .getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                // r will be populated with the coordinates of your view
                // that area still visible.
                activityRootView.getWindowVisibleDisplayFrame(r);
                int heightDiff = activityRootView.getRootView().getHeight()
                        - (r.bottom - r.top);
                Toast.makeText(TestActivity.this, String.valueOf(heightDiff),
                        Toast.LENGTH_SHORT).show();

            }
        });

以上代码位于我的TestActivity的onResume()中,主题为@android:style/Theme.NoTitleBar.Fullscreen

视图的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aaa" />

</LinearLayout>

menifest中的活动配置:

<activity
    android:name=".ui.activity.TestActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

答案 1 :(得分:0)

我用它来检测softInput的可见性。试试这个:

public boolean isKeyBoardVisible;
    int previousHeightDiffrence = 0;

    public void checkKeyboardHeight(final View parentLayout) {

        parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {

                        Rect r = new Rect();
                        parentLayout.getWindowVisibleDisplayFrame(r);

                        int screenHeight = parentLayout.getRootView()
                                .getHeight();
                        int heightDifference = screenHeight - (r.bottom);

                        if (previousHeightDiffrence - heightDifference > 50) {

                            // Keyboard is not visible
                        }
                        if (previousHeightDiffrence - heightDifference < -50) {

                            // Keyboard Visible
                        }

                        previousHeightDiffrence = heightDifference;
                        if (heightDifference > 100) {

                            isKeyBoardVisible = true;

                        } else {

                            isKeyBoardVisible = false;

                        }

                    }
                });

    } 

这里parentLayout是我活动的最顶层布局,如果键盘可见,则heightDifference是我键盘的高度......

答案 2 :(得分:0)

试试这个:

布局:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <MyView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

然后实现MyView

public class MyView extends View
{
     public static int ScreenHeight;

     @Override
     public function onDraw(Canvas canvas){
          if(ScreenHeight != canvas.getHeight()){
              // this is your event
          } 
          ScreenHeight = canvas.getHeight();

     }
}

现在ScreenHeight正是您要找的。