如何使片段荣誉软输入模式的活动

jph*_*jph 5 android android-layout android-softkeyboard android-fragments android-fragmentactivity

我在清单中配置了如下活动:

android:windowSoftInputMode="stateHidden|adjustPan"
Run Code Online (Sandbox Code Playgroud)

此活动创建一个片段.该片段将自身配置为onCreate()的全屏,例如:

setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);
Run Code Online (Sandbox Code Playgroud)

片段的布局大致如下:

<LinearLayout>
  <!-- a fixed height header -->
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.0">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <!-- some EditTexts -->
    </LinearLayout>
  </ScrollView>
  <!-- a fixed height footer -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

不幸的是,当显示片段时,软键盘会自动显示,输入模式为"adjustResize"而不是"adjustPan".这会导致页脚始终可见; 当键盘显示时,ScrollView的高度会缩小.

如何配置片段以具有"stateHidden | adjustPan"行为?如果重要的话,我从支持库中获取片段功能.

Man*_*ani 19

活动和屏幕上的软键盘都有自己的窗口. android:windowSoftInputMode表示活动的主窗口应如何与屏幕软键盘的窗口交互.

我相信你在用DialogFragment.它有自己的窗口,浮动在活动窗口的顶部.因此,为Activity通过设置的标志android:windowSoftInputMode不适用于DialogFragment.

一种可能的解决方案是以DialogFragment编程方式为窗口设置这些标志.使用getDialog检索基础对话框实例,使用getWindow检索对话框的窗口,并使用setSoftInputMode指定软输入模式的标志.

public static class MyDialog extends DialogFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Dialog layout inflater code
        // getDialog() need to be called only after onCreateDialog(), which is invoked between onCreate() and onCreateView(). 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
       // return view code
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于后代:设置软输入模式需要在片段的onCreateView()方法中进行,而不是在onCreate()上进行.如下:getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); (2认同)