当显示键盘时,向上推除除某些视图以外的内

Pha*_*inh 18 android view android-softkeyboard android-edittext

我有一个包含5 EditText和a Button和a TextView底部的布局.现在,当我按下时EditText,键盘将会显示,而我的所有键盘都会View向上推.

现在,我不想把我的TextViewButton上面的键盘,仅仅只希望所有的推升 EditTextScrollView到键盘上方.

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 1"

                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 2"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 3"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 4"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 5"
                android:inputType="textNoSuggestions"
                />

        </LinearLayout>

    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="I don't want to push this TextView and Button to above keyboard when keyboard is shown. Just obly want to push the ScrollView that contain all EditText"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我有个主意.当键盘显示和隐藏时我会倾听.当键盘显示我将设置ScrollView的下边距=键盘高度,当键盘隐藏我将设置此margin = 0.
有没有更容易处理我的情况?任何帮助或建议将非常感谢.

更新
如果我使用windowSoftInputMode=adjustPan=>并非所有EditText都推到键盘上方
如果我使用windowSoftInputMode=adjustResize=> Button,TextView所有EditText都推到键盘上方

Gab*_*han 5

无法使用-没有可靠的API可以检测到何时显示键盘。您会在此站点上看到“解决方案”,但是它们具有错误的肯定和否定。但这对您来说似乎最好的事情是将softInputMode设置为AdjustPan。这将使操作系统以使光标在键盘上方可见所需的最小量滚动整个屏幕。(整个应用程序在键盘上方的位置取决于模式AdjustResize)。


W4R*_*0CK 5

您必须添加android:windowSoftInputMode="adjustPan"您的Manifest,它将像专业人士一样工作:

<activity android:name=".your_activity_name"
        android:windowSoftInputMode="adjustPan">
    ..............

</activity>
Run Code Online (Sandbox Code Playgroud)


Anu*_*ngh 5

  1. 在所有Nexus移动和平板电脑设备上测试过.它工作正常.我正在使用您的布局xml代码,并且只在视图中添加了id.

  2. 我在我的生产应用程序中使用以下库,它非常有前景.要实现相同的效果并在app gradle中添加以下行:'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.0.1' 这是相同的链接.

  3. 有关其余代码,请参阅以下要点:清单代码,活动代码,活动布局代码.


Pha*_*inh 0

借助新功能,WindowInsetsCompat可以轻松检测键盘可见性(我在这里遵循答案/sf/answers/4446237141/)。现在我可以通过结合使用来解决这个问题,而不是像以前那样使用不太复杂
windowSoftInputMode=adjustPanwindowSoftInputMode=adjustResizeWindowInsetsCompat

ViewCompat.setOnApplyWindowInsetsListener(root) { v, insets ->
    val isKeyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
    
    if (isKeyboardVisible) {
        // Hide some view when keyboard visible
    } else {
        // Show it again here
    }
}
Run Code Online (Sandbox Code Playgroud)

Android清单

<activity android:name=".activity_name"
        android:windowSoftInputMode="adjustResize"> // all content will push to above keyboard then we able to scroll to see whole content
   
</activity>
Run Code Online (Sandbox Code Playgroud)