【问题标题】:How to make windowSoftInputMode="adjustPan" pan more如何使 windowSoftInputMode="adjustPan" 平移更多
【发布时间】:2012-01-25 04:18:50
【问题描述】:

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

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

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

【问题讨论】:

  • 张贴你的屏幕截图,这样很容易理解问题

标签: android pan


【解决方案1】:

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

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

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

希望这个想法对你有用。

谢谢

【讨论】:

  • “把所有的控件都放在ScrollView”是什么意思?
  • 为帖子标题而来。你如何实际控制如何平移更多?平移和调整大小仅将页面向上移动,足以不隐藏当前聚焦的编辑文本。如何使它平移+一些边距?
【解决方案2】:

把你的EditText放在ScrollView里面,然后给属性adjustResize,而不是adjustPan,它会自动调整您的屏幕及其组件。

【讨论】:

    【解决方案3】:

    这是一个非常古老的帖子,但我最近在我的项目中也遇到了这个问题。我想出了一个完美解决这个案子的解决方案,所以我想分享一下以帮助别人避免我所经历的挣扎。

    将 windowSoftInputMode 设置为“调整大小”不起作用,因为我的应用程序是全屏的。(这是由于提到的一个 android 错误here)。即使它确实有效,我相信这个选项也不是很好的 UI。所以我还想让“调整平移”选项与“更多平移”一起使用,不幸的是 Android 没有提供调整此边距的 API。并且在editText中添加“paddingBottom”会使屏幕的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,这最终会导致您的布局变得不成比例并且不需要。

    【讨论】:

      【解决方案4】:

      当我触摸它时,我在屏幕底部有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>
      

      【讨论】:

        【解决方案5】:

        对于那些真正想要将页面向上平移的人。您可以在您的 rootView 上执行您自己的升档/降档逻辑。您需要自己计算要移动的距离。

        见这个例子https://github.com/yatw/SoftKeyboardAdjust

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

          • 正如我在问题中所说的,adjustResize 没有这样做。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-05
          • 1970-01-01
          • 1970-01-01
          • 2023-03-07
          • 2015-03-27
          相关资源
          最近更新 更多