16

多数のEditTextフィールドを含むアクティビティがあり、windowSoftInputModeがadjustPanに設定されています。これはほとんど私が望むことを実行しますが、レイアウトの下部にあるeditTextの場合、十分にパンしません。パンして複数行の編集テキストの一番上の行が表示されますが、Enterキーを押すとすぐに、カーソルが1行下に移動し、キーボードの下に隠れます。

とにかく、editTextの上部がウィンドウの上部に完全に表示されるように、さらにパンすることができますか?

AdjustResizeを使用しても、私が望むことは絶対にできません。パンは正しいことです。さらにパンする必要があります。

4

6 に答える 6

6

同じことが私にも起こりました。

私が解決したのは、すべてのコントロールをScrolllViewに配置することです。

そして、android:windowSoftInputModeadjustResizeを設定ます。したがって、キーボードが開いているときはいつでも画面をスクロールしてビューを調整し、Edittextは常に画面に表示されます。

このアイデアがあなたのために働くことを願っています。

ありがとう

于 2014-10-29T10:53:56.440 に答える
1

EditTextScrollView内に配置し、 adjustPanの代わりにプロパティadjustResizeを指定すると、画面とそのコンポーネントが自動的に調整されます。

于 2014-10-31T11:47:29.533 に答える
1

これは非常に古い投稿ですが、私のプロジェクトではごく最近、これにも苦労しました。私はこのケースを完全に解決する解決策を思いついたので、誰かが私が経験した苦労を避けるのを助けるために共有したいと思いました。

アプリケーションがフルスクリーンであったため、windowSoftInputModeを「AdjustResize」に設定しても機能しませんでした(これは、ここで説明したAndroidのバグが原因です)。それが機能したとしても、このオプションはあまり良いUIではないと思います。そのため、「パンの調整」オプションを「パンの追加」で機能させることも望んでいましたが、残念ながらAndroidにはこのマージンを調整するためのAPIが用意されていません。また、editTextに「paddingBottom」を追加すると、画面のUIのバランスが崩れます。

多くの調査の結果、次の手順に従ってこれを解決することができました。(私はこの非常に役立つミディアムポストを使用しました)

1-AndroidManifestからwindowSoftInputMode="adjustUnspecificed"を作成します。

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」と一緒に使用すると、このロジックが現在のスクロールポイントからパンを調整するため、意図したとおりに機能しません。たとえば、同じビューに2つの編集テキストがあり、ユーザーが最初のテキストをクリックし、Androidロジックからレイアウトを少し上にパンしてから、ここで実装したロジックからスクロールするとします。次に、ユーザーが2番目のビューをクリックすると、Androidロジックが現在のスクロールy値からパンするため、editTextに非常に近くなり、スクロールyが既に増加しているため、ロジックは機能しません。または、スクロールyをインクリメントし続けることもできます。これにより、レイアウトのバランスが崩れ、望ましくなくなります。

于 2020-12-17T09:52:28.593 に答える
0

画面の下部にあるエディットテキストをタッチするとソフトキーボードが開きますが、エディットテキストが非表示になるため、レイアウトを変更して(相対レイアウトを使用)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>
于 2012-01-25T06:28:08.427 に答える
0

実際にページをもっと上にパンしたい人のために。rootViewで独自のシフトアップ/シフトダウンロジックを実行できます。自分でシフトする距離を計算する必要があります。

この例を参照してくださいhttps://github.com/yatw/SoftKeyboardAdjust

于 2020-10-05T04:00:58.530 に答える
-1

画面を調整するために、この行をアクティビティに追加する必要があります。

        <activity android:name=".TodoEdit"
        android:windowSoftInputMode="adjustResize">
于 2014-10-19T17:44:17.860 に答える