5

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

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

所以输入模式调整不起作用,我不能使用OnGlobalLayoutListener

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

谢谢。

4

3 回答 3

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();

            }
        });

上面的代码在onResume()我的 TestActivity 的主题中@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>

清单中的活动配置:

<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>
于 2013-05-11T03:15:45.907 回答
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就是你要找的。

于 2013-05-16T12:34:47.443 回答
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 是我的键盘高度.....

于 2013-05-15T12:38:00.697 回答