软键盘遮挡了底部对话框。

11

我在Stack Overflow上搜寻了所有相关的问题,尝试了基本上所有的解决方案,但是一无所获。我想要一个带有几个EditText和其他组件的底部表单模态对话框,使用BottomSheetDialogFragment创建应该很容易。

我的问题是软键盘覆盖了对话框的一半。我改变的任何设置似乎都没有起到任何作用。API有什么改变吗?搜索也没有找到解决方案。当前发生的情况是:键盘不会覆盖当前选中的编辑文本,但是它会覆盖下面的对话框。我希望整个对话框在任何时候都可见并且在键盘打开时移动。

这是我的Fragment类:

public class GoalUpdateFormDialogFragment extends BottomSheetDialogFragment {

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

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

        assert getDialog().getWindow() != null;

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

        return view;
    }

}

这是我正在使用的对话框布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/goal_update_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:layout_behavior="@string/bottom_sheet_behavior"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/progress_update_hint"/>

        <EditText
            android:id="@+id/goal_progress_number_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/goal_progress_box_hint"
            android:inputType="number"/>

    </LinearLayout>

    <EditText
        android:id="@+id/goal_update_comment_box"
        style="@style/Widget.AppCompat.EditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="@string/comment"
        android:lines="4"
        android:maxLines="6"
        android:inputType="textMultiLine"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/image_attachment_file_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/image_file_name_placeholder"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"/>

        <ImageButton
            android:id="@+id/delete_attachment_button"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginStart="4dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/transparent"
            android:alpha="0.54"
            android:src="@drawable/ic_delete_black_24dp"/>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start">

        <ImageButton
            android:id="@+id/attach_file_button"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="@drawable/transparent"
            android:src="@drawable/ic_attach_file_black_24dp"
            android:alpha="0.54"/>

        <ImageButton
            android:id="@+id/attach_image_button"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="@drawable/transparent"
            android:layout_toEndOf="@id/attach_file_button"
            android:src="@drawable/ic_add_a_photo_black_24dp"
            android:alpha="0.54"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/max_one_attachment"
            android:layout_toEndOf="@id/attach_image_button"
            android:layout_centerVertical="true"
            android:layout_marginStart="10dp"
            android:alpha="0.5"/>

        <ImageButton
            android:id="@+id/submit_button"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_send_black_24dp"
            android:background="@drawable/transparent"
            android:layout_alignParentEnd="true"
            android:alpha="0.54"/>

    </RelativeLayout>

</LinearLayout>

这是包含对话框的活动布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.iepone.classroom.view.activities.StudentDetailActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/todo"
                android:layout_gravity="center"
                android:textAlignment="center"/>

            <Button
                android:id="@+id/pick_image_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pick image"
                android:onClick="onClickPickImage"/>

            <Button
                android:id="@+id/test_comment_box_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test comment box"
                android:onClick="onClickTestCommentBox"/>

            <ImageView
                android:id="@+id/test_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

编辑:根据要求,这是Android清单,已删除不相关的部分:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="REDACTED">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:label">
        <activity
            android:name=".view.activities.SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".view.activities.MainActivity"
            android:theme="@style/MainActivityTheme" />
        <activity
            android:name=".view.activities.StudentDetailActivity"/>
    </application>

</manifest>

请问您能否发布您的Android清单文件? - Abdul Aleem
@AbdulAleem 完成,清单已添加。 - Bassinator
3个回答

1
如果您在底部工作表上有非常大的用户界面,则最好使用具有固定大小的NestedScrollView;
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:background="@color/colorWhite"
    android:orientation="vertical">

......Other views

</android.support.v4.widget.NestedScrollView>

你可以在bottomSheet中使用类似以下的BottomSheetBehavior;
定义一个成员变量。
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    }
};

然后设置它

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.content_register, null);
    ButterKnife.bind(this, contentView);
    dialog.setContentView(contentView);

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = params.getBehavior();

    if( behavior != null && behavior instanceof BottomSheetBehavior ) {
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
        ((BottomSheetBehavior) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
    }
}

布局不是很大,永远不需要滚动。 - Bassinator
你尝试过使用嵌套滚动视图吗?我在底部表中有两个视图,它们在嵌套滚动视图上运行良好。 - ddassa
我刚试了一下你的代码,看起来运行良好。你用的是哪个设计库?我正在使用compile 'com.android.support:design:23.4.0' - ddassa

0
请尝试在您的清单活动中添加以下行:
android:windowSoftInputMode="adjustResize"

例如:

<activity
        android:name=".view.activities.StudentDetailActivity"
        android:windowSoftInputMode="adjustResize" />

你的对话框大小将会在键盘弹起时自动调整。希望能帮到你。


1
这将适用于整个活动,并可能破坏同一活动中的其他输入(如果有)。在我看来,这是Android中非常敏感的区域,因为我已经看到同样的方法在不同版本的Android上产生了不同的行为。我目前也遇到了同样的问题,尽管这有助于提供一些关于如何绕过问题的见解。https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java - SlyOtis

0

我遇到了完全相同的错误。 在我的情况下,这是华为设备特有的问题。 我的活动和对话框主题已经有了该选项:

<item name="android:windowSoftInputMode">adjustResize</item>

唯一需要的是:

<item name="android:windowIsFloating">false</item>

因此,最终底部弹出式对话框的样式为:

    <style name="CustomBottomSheetDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@null</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
    </style>

希望它有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接