显示键盘时向上推送内容,但某些视图除外

Push up content except some view when keyboard shown(显示键盘时向上推送内容,但某些视图除外)

本文介绍了显示键盘时向上推送内容,但某些视图除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局在底部包含 5 个 EditText 和一个 Button 和一个 TextView.现在,当我按下 EditText 时,键盘将显示并且 all 我的 View 向上推.

I have a layout that contains 5 EditText and a Button and a TextView at bottom. Now when I press an EditText then the keyboard will shown and all my View is push up.

现在我不想将我的 TextViewButton 推到键盘上方,只想全部推上 ScrollView 内的 EditText 到键盘上方.

Now I don't want to push my TextView and Button to above keyboard, just only want to push up all EditText inside ScrollView to above keyboard.

<?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>

我有个想法是.当键盘显示和隐藏时我会监听.当键盘显示时,我将设置 ScrollView 的底部边距 = 键盘高度,当键盘隐藏时,我将设置此边距 = 0.
有什么方法更容易处理我的案子吗?任何帮助或建议将不胜感激.

I have an idea is. When I will listener when keyboard show and hide. When keyboard show I will set the bottom margin of ScrollView = keyboard height, when keyboard hide I will set this margin = 0.
Is there any way easier to handle my case? Any help or suggestion would be great appreciated.

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

UPDATE
If I use windowSoftInputMode=adjustPan => not all EditText is push up to above keyboard
If I use windowSoftInputMode=adjustResize => Button, TextView and all EditText is push up to above keyboard

推荐答案

有了 WindowInsetsCompat 的新功能,可以帮助轻松检测键盘可见性(我在这里按照答案 https://stackoverflow.com/a/63517673/5381331).
而不是像以前那样使用 windowSoftInputMode=adjustPan 来降低复杂性,现在我可以通过将 windowSoftInputMode=adjustResizeWindowInsetsCompat

With the new feature of WindowInsetsCompat which help detecting keyboard visibility easily (I follow answer here https://stackoverflow.com/a/63517673/5381331).
Instead of using windowSoftInputMode=adjustPan for less complex like before, now I can solve this problem by combine windowSoftInputMode=adjustResize with WindowInsetsCompat

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
    }
}

AndroidManifest

AndroidManifest

<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>

这篇关于显示键盘时向上推送内容,但某些视图除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:显示键盘时向上推送内容,但某些视图除外

基础教程推荐