代码之家  ›  专栏  ›  技术社区  ›  Michał Ziobro

软键盘在上面按下按钮。如何修复它?

  •  0
  • Michał Ziobro  · 技术社区  · 7 年前

    我有一个带有SCORLVIEW的ConstraintLayout和它下面的按钮(连接到屏幕底部。当我在ScrollView中编辑编辑文本输入时。然后出现的键盘将我的ScrollView内容向上移动(所需的行为,以便我可以滚动到它的末尾),但它也会向上推按钮(不需要的行为)。

    我想我可以改变windowAdjustMode,也许我可以检测到键盘显示,然后隐藏这个按钮?但这两种解决方案并不完美。 enter image description here

    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>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   hsm59    7 年前

    这可能会有帮助,我自己还没有尝试过,请尝试将下面的代码添加到您的 activity 在你的舱单上贴上标签

    编辑-添加 stateHidden 要实现所需功能,按钮将位于底部,滚动视图中的元素可以滚动。

    android:windowSoftInputMode="adjustPan|stateHidden"
    

    从…起 Android Documentation - 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);
    
                    }
                });
    

    加上 heightDifference 通过以编程方式创建视图并设置其高度。

    编辑3-

    使用此选项隐藏键盘

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

    让我知道这是否有效。

    推荐文章