簡體   English   中英

軟鍵盤將其上方的按鈕向上推。 如何解決?

[英]Soft Keyboard pushed button up above it. How to fix it?

我在ConstraintLayout的下面有ScorllView和Button(連接到屏幕底部。當我在ScrollView中編輯EditText輸入時。然后出現的鍵盤將ScrollView內容向上移動(期望的行為,因此我可以滾動到它的末尾),但是它還會向上推按鈕(不良行為)。

我想我可以更改windowAdjustMode,也許我可以檢測到鍵盤顯示然后隱藏此按鈕? 但是這兩個解決方案都不完美。 在此處輸入圖片說明

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/submitButton"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="0dp">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <EditText /> goes here 
    </android.support.constraint.ConstraintLayout>
</ScrollView>
    <Button
        android:id="@+id/submitButton"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="0dp"
        android:text="@string/wizard_singup_step_submit_button"
        style="@style/FormSubmitButton"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>

這可能會有所幫助,但我自己還沒有嘗試過,請嘗試將以下代碼添加到清單中的activity標簽中

編輯-添加stateHidden以實現所需的功能,該按鈕將位於底部,並且可以滾動滾動視圖中的元素。

android:windowSoftInputMode="adjustPan|stateHidden"

來自Android文檔 adjustPan活動的主窗口未調整大小以為軟鍵盤騰出空間。 而是,窗口的內容會自動平移,以使當前焦點不會被鍵盤遮擋,並且用戶始終可以看到他們正在鍵入的內容。 通常,這不如調整大小那么可取,因為用戶可能需要關閉軟鍵盤才能到達窗口的模糊部分並與之交互。

編輯2-用於計算鍵盤高度的代碼

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    Log.d("Keyboard Size", "Size: " + heightDifference);

                }
            });

通過以編程方式創建一個View並設置其高度來添加heightDifference

編輯3-

用它來隱藏鍵盤

public static void hideKeyboardFrom(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

讓我知道這個是否奏效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM