簡體   English   中英

軟鍵盤覆蓋底部工作表對話框

[英]soft keyboard is covering bottom sheet dialog

我已經搜索了關於 SO 的所有相關問題,基本上嘗試了他們所有的解決方案,但一無所獲。 我想使用BottomSheetDialogFragment有一個底部表單模式對話框,其中包含一些EditText和其他組件,這很容易。

我遇到的問題是軟鍵盤覆蓋了一半的對話框。 我更改的設置似乎對任何事情都沒有任何影響。 API有什么變化嗎? 再多的谷歌搜索似乎都無法找到解決方案。 當前發生的情況:鍵盤不會覆蓋當前選擇的編輯文本,但會覆蓋其下方的對話框。 我希望整個對話框始終可見,並在鍵盤打開時移動。

這是我的片段類:

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:windowSoftInputMode="adjustResize"

例如:

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

鍵盤啟動時,將調整對話框大小。 希望能幫助到你

如果你在底層有一個非常大的ui,最好是使用固定大小的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>

還有一個BottomSheetBehavior,如下面的bottomSheet;

定義成員變量

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);
    }
}

我有完全相同的錯誤。 在我丑陋的情況下,它是特定於華為設備的。 我的活動和對話主題已經有了選項:

<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>

我希望它有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM