【问题标题】:Problems with EditText and soft keyboard in a fragment片段中的 EditText 和软键盘问题
【发布时间】:2012-12-26 08:29:33
【问题描述】:

我目前正在构建一个 android 应用程序,该应用程序使用户能够拍照并写下它的详细信息。该应用程序使用 Sherlock 库。

我已经实现了一个 SherlockFragment 来显示图像和一些 EditText 和 TextView 以使用户能够键入图像的信息。但是,当其中一个 EditText 处于焦点时,所有字段都被向上推,软键盘弹出。

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#FFFFFF">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:layout_above="@+id/textView"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="20dip"
        android:contentDescription="desc" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Title:"
        android:textStyle="bold"
        android:layout_above="@+id/editTextSimple" 
        android:layout_marginBottom="2dip"
        android:textSize="15sp"/>

     <EditText
        android:id="@+id/editTextSimple"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_marginBottom="4dip">
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Description:"
        android:textStyle="bold"
        android:layout_above="@+id/editTextSimple1" 
        android:layout_marginBottom="2dip"
        android:textSize="15sp"/>

    <EditText
        android:id="@+id/editTextSimple1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"  
        android:layout_marginBottom="12dip">
    </EditText>

    <Button android:textColor="#FF000000" 
        android:id="@+id/submitButton" 
        android:text="@string/memorySubmit" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="24dip"
        android:onClick = "submit"/>
</RelativeLayout>

据我了解,在一项活动中,必须将此&lt;activity android:windowSoftInputMode="...." &gt; 放在 Android 清单中,以便在调用软键盘时调整视图。但是,我们如何为片段设置这个?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    这适用于我使用多个布局和视图块来创建消息传递屏幕的场景。把它放在 onCreate() 希望这会有所帮助。

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    

    【讨论】:

    • 为我工作并拯救了我的一天。添加评论以便其他人可能知道这是有效的。
    • @kenodoggy 这在第一次打开时对我有用,但是当我单击并开始输入文本时,我的布局再次向上推。如何解决这个问题?如果你知道,请告诉我。
    • 这就是我要找的。从片段完美工作
    • 在片段中工作,但我想知道是将输入模式值设置为永久还是仅用于片段生命周期?离开片段后如何回到之前的\默认值?
    【解决方案2】:

    一个Activity 的所有片段与其父Activity 具有相同的行为,但如果您需要不同Fragments 的不同键盘行为,您可以从Fragment 代码中动态更改此属性,如下所示:

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.softInputMode.SOFT_INPUT_ADJUST_PAN);
    

    希望这会有所帮助!

    【讨论】:

    • 嗨 Evos!谢谢您的回答!不幸的是,它仍然无法正常工作。我认为问题可能是因为android:layout_above,虽然我不能太确定。
    • 您是否尝试在清单中设置此参数的不同变体?
    • 我做到了。我意识到问题出在布局本身而不是软键盘上。所以我实现了一个滚动视图并且它有效!无论如何感谢您的帮助。
    【解决方案3】:

    这可以在 Fragment 类中用于向上移动编辑文本并滚动到结尾。

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    

    【讨论】:

      【解决方案4】:
      android:windowSoftInputMode="adjustPan"
      

      与该活动相关的所有片段的行为都相同,如果您想更改特定片段的行为而不是使用以下片段

      getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
      

      【讨论】:

        【解决方案5】:

        试试这个getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        【讨论】:

        【解决方案6】:

        我的项目中也出现了这个问题,我使用片段和选项卡开发了我的项目,

        如果我们采用相对布局并保持上下依赖关系,最后使用

        按钮

        android:layout_alignParentBottom="true"

        然后它会出现问题,解决方案是在根线性或相对布局中使用 ScrollView,在 ScrollView 内部将一个子视图作为线性或相对,并在 RelativeLayout 中将所有视图设置为彼此下方(如果在子视图中采用相对)或者不需要在 Linear 中设置依赖。

        就是这样,它会起作用的...... :)

        【讨论】:

          【解决方案7】:

          如果您使用的是 Fragment,请使用

          getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
          

          如果您使用的是 DialogFragment,请使用这个

          getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
          

          【讨论】:

            【解决方案8】:
               android:launchMode="singleTask"
               android:windowSoftInputMode="adjustPan"
            

            清单中的这种更改对我来说很好

            【讨论】:

            • 这将适用于 Activity,但他正在寻找用户碎片的方式.....
            猜你喜欢
            • 2011-10-17
            • 2012-10-03
            • 2014-02-20
            • 2023-03-29
            • 2021-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多