如何使windowSoftInputMode =“adjustPan”泛更多

时间:2012-01-25 04:18:51

标签: android pan

我有一个活动,其上有一堆EditText字段,windowSoftInputMode设置为adjustPan,这主要是我想要的,但是对于布局底部的editText,它不够平衡。它将平移以显示多行编辑文本的顶行,但只要您按Enter键,光标就会向下移动一行,现在隐藏在键盘下方。

无论如何我可以让它进一步平移,以便editText的顶部一直在窗口的顶部。

使用adjustResize绝对不会做我想要的,pan是正确的,只需要它进一步平移。

6 个答案:

答案 0 :(得分:6)

同样的事情发生在我身上。

我要解决的问题是,我只是将所有控件都放在 ScrolllView 中。

并设置android:windowSoftInputMode adjustResize 。因此,无论何时打开键盘,它都会滚动屏幕以调整视图,并且 Edittext 始终显示在屏幕上。

希望这个想法适合你。

由于

答案 1 :(得分:1)

EditText 放入 ScrollView ,然后提供属性 adjustResize ,而不是 adjustPan ,它会自动调整屏幕及其组件。

答案 2 :(得分:0)

当我触摸它打开软键盘时,我在屏幕底部有edittext但是它隐藏了edittext,所以在布局中进行更改(使用相对布局),它对我有用,而不使用adjustPan。这是我的XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gen_mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/headerlinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/navigation_bar" >

            <Button
                android:id="@+id/chatterBack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:background="@drawable/navigation_cancel_button"
                android:textColor="#FFFFFF" >
            </Button>

            <TextView
                android:id="@+id/ittletextView"
                style="@style/titleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Title" >
            </TextView>

            <Button
                android:id="@+id/blockhide"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="20dp"
                android:background="@drawable/navigation_cancel_button"
                android:padding="5dp"
                android:text="Block/Hide"
                android:textColor="#FFFFFF" >
            </Button>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/middlelinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/headerlinearLayout"
            android:orientation="vertical" >

        </LinearLayout>


        <LinearLayout
            android:id="@+id/footerlinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/commnet_post_bg"
            android:gravity="center"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >


            <EditText
                android:id="@+id/sendeditText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="@drawable/comment_post_text_box"
                android:imeOptions="actionSend|flagNoEnterAction"
                android:inputType="text"
                android:paddingLeft="10dp"
                android:paddingRight="5dp"
                android:singleLine="true" >
            </EditText>

            <Button
                android:id="@+id/sendButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/send_button"
                android:text="Send"
                android:textColor="#FFFFFF" >
            </Button>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

答案 3 :(得分:0)

对于那些实际上想进一步放大页面的人。您可以在rootView上执行自己的上移/下移逻辑。您需要自己计算要移动多少距离。

请参见以下示例https://github.com/yatw/SoftKeyboardAdjust

答案 4 :(得分:0)

这是一个非常古老的帖子,但是最近我在我的项目中也为此付出了很多努力。我想出了一种可以完美解决此问题的解决方案,因此我想分享一下,以帮助某人避免经历的艰辛。

由于我的应用程序为全屏显示,因此未将windowSoftInputMode设置为“调整调整大小”。(这是由于提到了here的一个Android错误)。即使它确实起作用,我相信该选项也不是很好的UI。因此,我还想使“调整平移”选项与“更多平移”一起使用,不幸的是,Android没有提供API来调整此余量。并将“ paddingBottom”添加到editText会使屏幕的UI不平衡。

经过大量研究,我可以按照以下步骤解决此问题; (我使用了this很有帮助的中篇文章)

1-从AndroidManifest中创建windowSoftInputMode =“ adjustUnspecified”。

2-添加以下方法作为扩展功能;

fun Activity.getRootView(): View {
    return findViewById<View>(android.R.id.content)
}
fun Context.convertDpToPx(dp: Float): Float {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        dp,
        this.resources.displayMetrics
    )
}
fun Activity.isKeyboardOpen(): Boolean {
    val visibleBounds = Rect()
    this.getRootView().getWindowVisibleDisplayFrame(visibleBounds)
    val heightDiff = getRootView().height - visibleBounds.height()
    val marginOfError = Math.round(this.convertDpToPx(50F))
    return heightDiff > marginOfError
}

3-创建全局布局侦听器,以检测何时键盘可见,如下所示;

  val listener = object : ViewTreeObserver.OnGlobalLayoutListener {
        // Keep a reference to the last state of the keyboard
        private var lastState: Boolean = isKeyboardOpen() ?: false
        /**
         * Something in the layout has changed
         * so check if the keyboard is open or closed
         * and if the keyboard state has changed
         * save the new state and invoke the callback
         */
        override fun onGlobalLayout() {
            val isOpen = isKeyboardOpen() ?: false
            if (isOpen == lastState) {
                return
            } else {
                onKeyboardStateChanged(isOpen)
                lastState = isOpen
            }
        }
    }

4-在onKeyboardStateChanged方法中,我通过screenHeight / 4向上滚动了我的主要活动布局,这通常就足够了,但是您可以按自己认为合适的方式进行滚动,然后将布局更改为所需的任何布局滚动。另外,我使用过渡动画来使过渡更平滑,这对于功能而言不是必需的。

private fun onKeyboardStateChanged(open: Boolean) {
    runOnUiThread {
        if (open) {
            val screenHeight = resources.displayMetrics.heightPixels
            TransitionManager.beginDelayedTransition(main_activity_layout as ViewGroup)
            main_activity_layout.scrollTo(0, screenHeight / 4)
        } else {
            TransitionManager.beginDelayedTransition(main_activity_layout as ViewGroup)
            main_activity_layout.scrollTo(0, 0)
        }
    }
}

5-添加视图以在您的 onResume 上侦听,并在 onPause 上删除侦听器,如下所示;

 override fun onResume() {
        super.onResume()
        val view = getRootView()
        view.viewTreeObserver?.addOnGlobalLayoutListener(listener)
    }

    override fun onPause() {
        super.onPause()
        val view = getRootView()
        view.viewTreeObserver?.removeOnGlobalLayoutListener(listener)
    }
  • 如果将 windowSoftInputMode 设置为“ adjustNothing”,则此操作将无效。因为侦听器逻辑将失败。

  • 此外,如果将它与“ adjustPan”一起使用,它将无法按预期工作,因为此逻辑会从当前滚动点调整平移。假设您在同一个视图中有两个编辑文本,用户单击第一个,然后从Android逻辑中稍微平移布局,然后从此处实现的逻辑中滚动。然后,用户单击第二个视图,Android逻辑从当前滚动y值进行平移,因此它再次非常接近editText,并且我们的逻辑不起作用,因为滚动y已经增加。另外,您可以继续增加滚动y,滚动y最终会导致布局不成比例,这是不希望的。

答案 5 :(得分:-1)

您需要将此行添加到您的活动中以调整屏幕。

        <activity android:name=".TodoEdit"
        android:windowSoftInputMode="adjustResize">