首页 > 解决方案 > 点击ViewPager的editText时软键盘一直关闭

问题描述

我有一个片段,其中包含 2 个片段的 ViewPager2。在第二个子片段中,我有一个 recyclerview,它的 viewholder 包含 TextView 和 EditText。

在模拟器中,当我单击 EditView 时,软键盘一直关闭。

在真实设备(Sony xperia XA)中,软键盘没有关闭,但无论我单击另一个 EditText,它始终专注于第一个编辑文本

我曾尝试使用 android:windowSoftInputMode="adjustPan" 和 android:windowSoftInputMode="adjustResize" 但它不能解决问题。

我在 LinearLayout 之外使用 DataBinding 和 ConstraintLayout

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="vn.com.ntn.obee.screens.fragments.exercise.InputQuestionFragment">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_medium"
        android:layout_marginEnd="@dimen/margin_medium"
        android:layout_marginBottom="@dimen/margin_medium"
        android:paddingTop="@dimen/margin"
        android:paddingBottom="@dimen/margin_medium"
        app:cardCornerRadius="20dp"
        app:cardElevation="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutQuestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/question_drawable"
            android:orientation="vertical"
            android:paddingTop="@dimen/margin"
            android:paddingBottom="@dimen/margin"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent">

            <vn.com.ntn.obee.screens.views.SelectableTextView
                android:id="@+id/tvQuestion"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingMultiplier="1.6"
                android:maxHeight="200dp"
                android:paddingStart="@dimen/margin_medium"
                android:paddingEnd="@dimen/margin_medium"
                android:paddingTop="@dimen/margin"
                android:paddingBottom="@dimen/margin_medium"
                android:scrollbars="vertical"
                android:fontFamily="@font/proxima_nova_bold"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_normal"
                app:htmlText="@{answer.content ?? answerResult.contentExtend}"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/etAnswer"
                android:layout_width="match_parent"
                android:layout_height="@dimen/size_icon_x5"
                android:hint="@string/enter_your_answer_here"
                android:paddingStart="@dimen/margin_medium"
                android:paddingEnd="@dimen/margin_medium"
                android:paddingBottom="2dp"
                android:layout_marginStart="@dimen/margin_medium"
                android:layout_marginEnd="@dimen/margin_medium"
                android:layout_marginBottom="@dimen/margin_small"
                android:background="@drawable/bg_input_question_edittext"
                android:fontFamily="@font/proxima_nova_bold"
                android:textColor="@color/pink"
                android:textColorHint="#FFC9C9"
                android:textSize="@dimen/text_size_medium"
                android:singleLine="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvQuestion"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintVertical_bias="1"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

我认为这些日志可能有我发现问题:

2020-10-12 00:20:43.527 3051-3051/? I/KeyboardViewUtil: KeyboardViewUtil.getKeyboardHeightRatio():128 systemKeyboardHeightRatio:1.000000; userKeyboardHeightRatio:1.000000.
2020-10-12 00:20:43.528 3051-3051/? I/AndroidIME: AbstractIme.onActivate():83 LatinIme.onActivate() : EditorInfo = Package = com.nghianguyen.obee_android.debug : Type = Text : Learning = Enable : Suggestion = Show : AutoCorrection = Enable : Microphone = Show : NoPersonalizedLearning = Disable, IncognitoMode = false
2020-10-12 00:20:43.529 3051-3051/? I/Delight5Facilitator: Delight5Facilitator.initializeForIme():572 initializeForIme() : Locale = [en_US], layout = qwerty
2020-10-12 00:20:43.529 3051-3051/? I/VoiceInputManagerWrapper: VoiceInputManagerWrapper.cancelShutdown():54 cancelShutdown()
2020-10-12 00:20:43.529 3051-3051/? I/VoiceInputManagerWrapper: VoiceInputManagerWrapper.syncLanguagePacks():66 syncLanguagePacks()
2020-10-12 00:20:43.557 3051-6376/? I/SpeechFactory: SpeechRecognitionFactory.maybeScheduleAutoPackDownloadForFallback():155 maybeScheduleAutoPackDownloadForFallback()
2020-10-12 00:20:43.569 1845-1845/? E/servicemanager: Could not find android.hardware.power.IPower/default in the VINTF manifest.
2020-10-12 00:20:43.596 3051-3051/? I/KeyboardWrapper: KeyboardWrapper.consumeEvent():264 Skip consuming an event as current keyboard is deactivated (state=0, keyboard existence=true)
2020-10-12 00:20:43.669 3051-3051/? W/KeyboardModeManager: KeyboardModeManager.setInputView():359 setInputView() : inputView = com.google.android.apps.inputmethod.libs.framework.core.InputView{cb5f35e V.E...... ......ID 0,0-1080,2022}
2020-10-12 00:20:43.672 3051-3051/? I/ConversationToQueryExtension: ConversationToQueryExtension.isEnabledForHostApp():254 Conv2Query not enabled due to current app [com.nghianguyen.obee_android.debug] not in whitelist
2020-10-12 00:20:43.672 3051-3051/? I/ConversationToQueryExtension: ConversationToQueryExtension.onActivate():165 onActivate() : Disabled by unsupported host app
   

标签: androidandroid-recyclerviewandroid-edittextandroid-softkeyboard

解决方案


问题是我使用了太多的约束布局。我将约束布局用于:

  • ViewPager 的子片段。
  • 回收站视图的项目。

我将片段的布局更改为 LinearLayout 和项目的 FrameLayout 解决了我的问题。

注意:使用 NestedScrollView 包装我的回收站视图解决了我的问题,但它阻止了我滚动回收站视图


推荐阅读