简体   繁体   English

DialogFragment 中的 ViewPager2 内的 EditText 不会显示键盘

[英]Keyboard won't show for EditText inside a ViewPager2 in a DialogFragment

I have a layout - let's call it layout A - with some EditText s, which I inflate inside a DialogFragment and all goes well with them.我有一个布局 - 让我们称之为布局 A - 带有一些EditText s,我在DialogFragment中膨胀它们并且一切顺利。 I have another DialogFragment whose layout - let's call it layout B - has a ViewPager2 in it, and I use it with a FragmentStateAdapter which hosts fragments where I reuse layout A for each of them.我有另一个DialogFragment ,其布局 - 让我们称之为布局 B - 中有一个ViewPager2 ,我将它与FragmentStateAdapter一起使用,它承载片段,我在其中为每个片段重用布局 A。

Now my problem is that when I tap on the EditText s inside the ViewPager2 , they seem to be getting focus (my grasp of that subject isn't perfect), the cursor starts showing and I can manipulate the text with it ("select", "select all", "cut", etc.), but the soft keyboard won't open up.现在我的问题是,当我点击ViewPager2中的EditText时,它们似乎得到了关注(我对该主题的掌握并不完美),cursor 开始显示,我可以用它来操作文本(“选择” 、“全选”、“剪切”等),但软键盘打不开。 In the dialog without the viewPager, those same EditText s work flawlessly and the keyboard opens up.在没有 viewPager 的对话框中,那些相同的EditText可以完美地工作并且键盘会打开。

In the Android Manifest I have android:windowSoftInputMode="adjustPan" (I use a single activity in my app).在 Android 清单中,我有android:windowSoftInputMode="adjustPan" (我在我的应用程序中使用了一个活动)。

What could be the problem?可能是什么问题呢?

My code:我的代码:

Layout A:布局一:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/double_margin"
        android:layout_marginLeft="@dimen/double_margin"
        android:text="@string/title_colon"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@id/title_edit_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/title_edit_text" />

    <EditText
        android:id="@+id/title_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/double_margin"
        android:layout_marginRight="@dimen/double_margin"
        android:autofillHints="false"
        android:ellipsize="end"
        android:hint="@string/enter_track_title"
        android:inputType="textCapWords|textMultiLine"
        android:maxLines="2"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@id/position_edit_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/title_text_view"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/position_text_view"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/double_margin"
        android:layout_marginLeft="@dimen/double_margin"
        android:text="@string/number_dot"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@id/position_edit_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/position_edit_text" />

    <EditText
        android:id="@+id/position_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin"
        android:layout_marginEnd="@dimen/double_margin"
        android:layout_marginRight="@dimen/double_margin"
        android:layout_marginBottom="@dimen/margin"
        android:autofillHints="false"
        android:ellipsize="end"
        android:hint="@string/enter_track_position"
        android:inputType="text"
        android:maxLength="6"
        android:maxLines="1"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@id/duration_text_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/position_text_view"
        app:layout_constraintTop_toBottomOf="@id/title_edit_text"
        tools:ignore="TextFields" />

    <TextView
        android:id="@+id/duration_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/duration_colon"
        android:textStyle="bold"
        android:layout_marginBottom="@dimen/double_margin"
        app:layout_constraintBottom_toTopOf="@id/colon_text_view"
        app:layout_constraintEnd_toEndOf="@id/seconds_picker"
        app:layout_constraintStart_toStartOf="@id/minutes_picker"
        app:layout_constraintTop_toBottomOf="@id/position_edit_text" />

    <NumberPicker
        android:id="@+id/minutes_picker"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/double_margin"
        android:layout_marginLeft="@dimen/double_margin"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/colon_text_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/duration_text_view" />

    <TextView
        android:id="@+id/colon_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        android:text="@string/colon"
        android:textSize="42sp"
        app:layout_constraintBottom_toBottomOf="@id/minutes_picker"
        app:layout_constraintEnd_toStartOf="@id/seconds_picker"
        app:layout_constraintStart_toEndOf="@id/minutes_picker"
        app:layout_constraintTop_toTopOf="@id/minutes_picker"
        app:layout_constraintVertical_bias="0.42000002" />

    <NumberPicker
        android:id="@+id/seconds_picker"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/double_margin"
        android:layout_marginRight="@dimen/double_margin"
        app:layout_constraintBottom_toBottomOf="@id/minutes_picker"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/colon_text_view"
        app:layout_constraintTop_toTopOf="@id/minutes_picker" />


</androidx.constraintlayout.widget.ConstraintLayout>

Layout B:布局 B:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/edit_track_divider_1"
        style="@style/divider"
        android:layout_marginTop="@dimen/double_margin"
        app:layout_constraintBottom_toTopOf="@id/left_arrow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/left_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/quadruple_margin"
        android:layout_marginLeft="@dimen/quadruple_margin"
        android:layout_marginTop="@dimen/double_margin"
        android:background="@android:color/transparent"
        android:contentDescription="@string/left_arrow_image"
        android:scaleX="1.5"
        android:scaleY="1.5"
        app:layout_constraintBottom_toTopOf="@id/edit_tracks_pager"
        app:layout_constraintEnd_toStartOf="@id/right_arrow"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/edit_track_divider_1"
        app:srcCompat="@drawable/ic_action_left" />

    <TextView
        android:id="@+id/position_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@id/left_arrow"
        app:layout_constraintEnd_toStartOf="@id/right_arrow"
        app:layout_constraintStart_toEndOf="@id/left_arrow"
        app:layout_constraintTop_toTopOf="@id/left_arrow" />

    <ImageButton
        android:id="@+id/right_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/quadruple_margin"
        android:layout_marginRight="@dimen/quadruple_margin"
        android:background="@android:color/transparent"
        android:contentDescription="@string/right_arrow_image"
        android:scaleX="1.5"
        android:scaleY="1.5"
        app:layout_constraintBottom_toBottomOf="@+id/left_arrow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/left_arrow"
        app:layout_constraintTop_toTopOf="@+id/left_arrow"
        app:srcCompat="@drawable/ic_action_right" />


    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/edit_tracks_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/edit_track_divider_2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/left_arrow" />

    <View
        android:id="@+id/edit_track_divider_2"
        style="@style/divider"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/edit_tracks_pager" />


</androidx.constraintlayout.widget.ConstraintLayout>

I eventually found some "hackish" unsatisfying way to fix this issue... I have added a dummy EditText in the layout right above the ViewPager2 and set its visibility to gone .我最终找到了一些“hackish”不令人满意的方法来解决这个问题......我在ViewPager2正上方的布局中添加了一个虚拟EditText并将其可见性设置为gone Now the EditText s work as expected - ie the keyboard opens up.现在EditText按预期工作 - 即键盘打开。 I have no idea why this works and therefore still don't know what caused the problem.我不知道为什么会这样,因此仍然不知道是什么导致了问题。 I found this after playing around hopelessly for solutions.在无可救药地寻找解决方案后,我发现了这一点。 If anyone knows what caused the problem, why my solution practically solves it and what is the correct way to solve this - please let me know.如果有人知道是什么导致了问题,为什么我的解决方案实际上解决了这个问题以及解决这个问题的正确方法是什么 - 请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM