29

我正在尝试使用 aSherlockDialogFragment来询问用户的一些输入。在我的手机(Galaxy Nexus,4.2)上一切正常,但在较小的手机(模拟器 2.3.3)上,当键盘出现时,它会覆盖 DialogFragment 的两个按钮,如下所示:

被键盘覆盖的对话框

我的布局在 ScrollView 内,我将 softInputMode 更改为SOFT_INPUT_ADJUST_RESIZE我的onViewCreated. 我也试过SOFT_INPUT_ADJUST_PAN了,还是不行

MyCustomDialog.java

public class AddTaskDialog extends SherlockDialogFragment implements OnDateSetListener{
//...
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        this.inflater =  getActivity().getLayoutInflater();
        View mainView =inflater.inflate(R.layout.custom_dialog, null);
        builder.setView(mainView);
        this.taskNote = (EditText) mainView.findViewById(R.id.ET_taskNote);
        this.taskText = (EditText) mainView.findViewById(R.id.ET_taskText);
        this.taskValue = (EditText) mainView.findViewById(R.id.ET_taskValue);
        /*
         * Other stuff
         */
        builder.setTitle(getString(R.string.new_task, hType.toString()))
               .setPositiveButton(R.string.dialog_confirm_button, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    //...
                    }
               })
               .setNegativeButton(R.string.dialog_cancel_button, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

这是我的布局:

custom_dialog.xml

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@color/abs__background_holo_light">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin">
        <TextView
            android:id="@+id/TV_taskText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/task_text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <EditText
            android:id="@+id/ET_taskText"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/create_task_hint"
            android:inputType="textNoSuggestions"
            android:singleLine="true" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin" >
        <TextView
            android:id="@+id/TV_taskNote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/task_note"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/ET_taskNote"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:minLines="2"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textMultiLine"
            android:hint="@string/task_note_hint">

        </EditText>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/repeat_days"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_gravity="top"
        android:orientation="horizontal"
        android:visibility="gone"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin">
        <!-- Day buttons are put here programatically -->
    </LinearLayout>
</LinearLayout>

那么,你能帮助我,并指导我如何显示这些按钮吗?要么平移视图,要么让它调整大小......

4

10 回答 10

49

我只是在 DialogFragment 中使用以下行:

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

仅此而已,请参见此处的完整示例:

    public class TextEditor extends DialogFragment {

    public TextEditor () {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        //set to adjust screen height automatically, when soft keyboard appears on screen 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        //[add more custom code...]
        return view;
    }
}
于 2014-10-17T22:45:15.800 回答
32

在使用对话框片段的活动中将该windowSoftInputMode属性设置为。adjustNothingAndroidManifest.xml

<activity
    ...
    android:windowSoftInputMode="adjustNothing">
...

onCreateDialog隐藏软输入:

...
Dialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return dialog;
}

仅供参考:https ://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

于 2013-06-25T16:46:34.313 回答
10

确保布局在滚动视图内:

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  -->your layout here 
</ScrollView>

并关注Dirk 评论

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_text_editor, container);

//add this line 
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

      //[add more custom code...]
      return view;
    }
于 2016-03-29T21:39:07.323 回答
9

即使回复有点晚了,由于问题在 DialogFragment 中,下面的代码解决了我的问题。

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    // Setting STYLE_NO_FRAME allows popup dialog fragment to resize after keyboard is shown
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.theme_popupdialog_style);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setCanceledOnTouchOutside(false);

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    return dialog;
}

至于样式主题,我应用了以下代码

/** must put parent="@android:style/Theme.Dialog for it to work */
<style name="theme_popupdialog_style" parent="@android:style/Theme.Dialog">
    <item .... >...</item>
</style>
于 2017-05-03T10:43:12.460 回答
8

如果有人对 BottomSheetDialog 有类似的问题。这个解决方案就像一个魅力。

内部款式:

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style>

android:windowIsFloating应该是false

android:windowSoftInputMode必须是adjustResize.

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_corner_dialog</item>
</style>

在 NestedScrollView 内包装布局

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

     <--Rest of the layout-->
</androidx.core.widget.NestedScrollView>

在某些设备上,这种解决方案还不够。将此添加到代码中完全解决了我的问题。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.setOnShowListener {
        val dialog = it as BottomSheetDialog
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet?.let { sheet ->
            dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
            sheet.parent.parent.requestLayout()
        }
    }
}
于 2019-06-17T13:08:37.103 回答
7

这也可能是由以下原因引起的:

<item name="android:windowTranslucentStatus">true</item>

尝试将其从您的主题中删除。

于 2018-01-18T18:56:45.963 回答
4

除了其他答案中提到的更改还检查了dialogfragment的主题
从我的实验中,“ android:windowIsFloating ”属性似乎会影响窗口对软输入的反应。

如果您将此设置为 false,则当键盘可见时,窗口不会向上滑动。

于 2017-03-23T07:24:39.800 回答
3

要正常使用 AutoCompleteTextView DialogFragment 不能设置为全屏。相反,您可以在样式中将宽度设置为 match_parent。下面的示例代码:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
    return inflater.inflate(R.layout.dialog_filter, container)
}

override fun getTheme(): Int {
    return R.style.AlertDialog_FullWidth
}

款式:

<style name="AlertDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.8</item>
    <item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
</style>

<style name="AlertDialog.FullWidth" parent="AlertDialog">
    <item name="android:layout_width">match_parent</item>
    <item name="android:windowIsFloating">false</item>
</style>
于 2020-09-18T09:13:12.460 回答
0

正如已经提到的,android:windowSoftInputMode="adjustResize"并且dialog.getWindow().setSoftInputMode(WIndowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);是这样做的正确方法。

但。如果您的视图根本无法调整大小,那么底部的按钮仍将被隐藏。就我而言,这个 hack就足够了:

键盘隐藏

我设置android:layout_weight了顶视图,这样当键盘打开并调整对话框大小时——顶视图将被隐藏: 显示键盘

于 2018-08-13T11:36:51.873 回答
-1

对于DialogFragment,似乎应用SoftInputMode仅在从类内部设置时才有效DialogFragment,而不是在调用者类中设置:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);    

}

此外,对于onStart方法,我添加了以下内容以水平扩展对话框布局:

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
于 2018-06-01T03:11:46.667 回答