【问题标题】:DialogFragment not resizing for soft keyboard on >= API 24DialogFragment 未在 >= API 24 上调整软键盘的大小
【发布时间】:2017-07-12 16:09:48
【问题描述】:

我已经用谷歌搜索和搜索 Stack Overflow 2 天,但没有找到可以帮助我的东西。

我正在尝试在智能手机上实现全屏的 DialogFragment,在平板电脑上实现模态对话框。

这是它的外观(在 API 级别 23 设备上拍摄):

这是它在 API 级别 24 设备上的外观:

这适用于 API 级别

TestDialogFragment.java:

public class TestDialogFragment extends DialogFragment
{
    private boolean isTablet;

    public TestDialogFragment()
    {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        isTablet = getResources().getBoolean(R.bool.isTablet); // defaults to false, true >= sw600dp
        if (isTablet)
        {
            setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
        } else
        {
            setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
        }
        setCancelable(false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
        savedInstanceState)
    {
        if (!isTablet)
        {
            Window window = getDialog().getWindow();
            if (window != null)
            {
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            }
        }

        return inflater.inflate(R.layout.dialog_fragment, container, false);
    }

    @Override
    public void onStart()
    {
        super.onStart();
        if (isTablet)
        {
            Dialog d = getDialog();
            if (d != null)
            {
                int width = ViewGroup.LayoutParams.WRAP_CONTENT;
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                d.getWindow().setLayout(width, height);
            }
        }
    }
}

dialog_fragment.xml:

<android.support.constraint.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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/dialog_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:ellipsize="end"
            android:fontFamily="sans-serif-medium"
            android:maxLines="2"
            android:paddingBottom="14dp"
            android:paddingTop="14dp"
            android:text="@string/dialog_title"
            android:textSize="20sp" />

    </android.support.v7.widget.Toolbar>

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:ems="10"
        android:hint="@string/edit_text_1"
        android:inputType="text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/progress_bar"
        app:layout_constraintTop_toBottomOf="@+id/dialog_toolbar"
        app:theme="@style/AppTheme" />

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:progressBarStyle"
        android:layout_width="24dp"
        android:layout_height="0dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@id/edit_text"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toRightOf="@id/edit_text"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/edit_text"
        app:layout_constraintWidth_default="wrap"
        tools:visibility="visible" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/results_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edit_text"/>

    <TextView
        android:id="@+id/no_results_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="32dp"
        android:gravity="center"
        android:text="@string/no_results"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/edit_text"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHeight_default="wrap"
        tools:visibility="visible" />
</android.support.constraint.ConstraintLayout>

我不明白为什么这不适用于 API 级别 24 及更高级别。我在 Android 24 的发行说明中看到此行为没有变化。

我的代码有什么问题还是我忽略了什么?

提前感谢您的帮助!

【问题讨论】:

    标签: android android-softkeyboard android-dialogfragment dialogfragment


    【解决方案1】:

    通过扩展 Mike Penz 的 MaterialDrawer library 中的 this 类,我设法至少让对话框的内容始终可见。这不是最漂亮的解决方案,但至少对话框中的所有内容都可以在键盘打开的情况下进行交互。

    代码如下:

    public class KeyboardUtil
    {
        private View activityDecorView;
    
        private View dialogDecorView;
    
        private View contentView;
    
        private int diff;
    
        //a small helper to allow showing the editText focus
        ViewTreeObserver.OnGlobalLayoutListener activityListener = new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                activityDecorView.getWindowVisibleDisplayFrame(r);
    
                //get screen height and calculate the difference with the usable area from  r
                int height = activityDecorView.getContext().getResources().getDisplayMetrics().heightPixels;
                diff = height - r.bottom;
    
                //no difference means no keyboard, so remove padding
                if (diff == 0)
                {
                    //check if the padding is != 0 (if yes reset the padding)
                    if (contentView.getPaddingBottom() != 0)
                    {
                        //reset the padding of the contentView
                        contentView.setPadding(0, 0, 0, 0);
                    }
                }
            }
        };
    
        ViewTreeObserver.OnGlobalLayoutListener dialogListener = new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                //if it could be a keyboard and the view hasn't shrunk on it's own add the padding to the view
                if (diff != 0 && activityDecorView.getContext().getResources().getDisplayMetrics().heightPixels - diff >
                        contentView.getHeight())
                {
                    //the view shrunk on it's own. remove padding
    
                    //check if the padding is != 0 (if yes reset the padding)
                    if (contentView.getPaddingBottom() != 0)
                    {
                        //reset the padding of the contentView
                        contentView.setPadding(0, 0, 0, 0);
                    }
                } else
                {
                    // if the usable screen height differs from the total screen height we assume that it shows a
                    // keyboard now
                    //check if the padding is 0 (if yes set the padding for the keyboard)
                    if (contentView.getPaddingBottom() != diff)
                    {
                        //set the padding of the contentView for the keyboard
                        contentView.setPadding(0, 0, 0, diff);
                    }
                }
            }
        };
    
        public KeyboardUtil(Activity act, View contentView)
        {
            this.activityDecorView = act.getWindow().getDecorView();
            this.contentView = contentView;
    
            //only required on newer android versions. it was working on API level 19
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                activityDecorView.getViewTreeObserver().addOnGlobalLayoutListener(activityListener);
            }
        }
    
        public KeyboardUtil(Activity act, Dialog dialog, View contentView)
        {
            this(act, contentView);
    
            if (dialog.getWindow() != null)
            {
                this.dialogDecorView = dialog.getWindow().getDecorView();
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
                {
                    dialogDecorView.getViewTreeObserver().addOnGlobalLayoutListener(dialogListener);
                }
            }
        }
    
        public void enable()
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                activityDecorView.getViewTreeObserver().addOnGlobalLayoutListener(activityListener);
                if (dialogDecorView != null)
                {
                    dialogDecorView.getViewTreeObserver().addOnGlobalLayoutListener(dialogListener);
                }
            }
        }
    
        public void disable()
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                activityDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(activityListener);
                if (dialogDecorView != null)
                {
                    dialogDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(dialogListener);
                }
            }
        }
    
    
        /**
         * Helper to hide the keyboard
         *
         * @param act
         */
        public static void hideKeyboard(Activity act)
        {
            if (act != null && act.getCurrentFocus() != null)
            {
                InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity
                        .INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-11
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 2011-05-16
      • 2021-02-19
      • 2015-11-04
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多