【问题标题】:android soft keyboard overlays the edittext in recyclerview in bottomsheetandroid软键盘覆盖bottomsheet的recyclerview中的edittext
【发布时间】:2017-02-17 17:08:22
【问题描述】:

我在RecyclerView 中有几个EditTextBottomSheetDialog 中。我现在遇到的问题是,当屏幕上显示BottomSheetDialog 时,我点击例如RecyclerView 中的第7 个EditText。软键盘出现并覆盖EditText,所以我看不到我输入的内容。但是如果我将BottomSheetDialog 向上拖动一点,EditText 将不会被软键盘覆盖,即使我点击屏幕上的最后一个EditTextRecyclerView 在这种情况下肯定会调整大小,但如果我不向上拖动 BottonSheetDialog 则不会调整大小。知道为什么吗?以及如何解决这个问题?

这就是它的样子。

Main.java

class VH extends RecyclerView.ViewHolder {
    public VH(View itemView) {
        super(itemView);
    }
}

private void test() {
    BSTest bsTest = new BSTest(this);
    bsTest.setContentView(R.layout.bottomsheet_test);
    RecyclerView rv = (RecyclerView) bsTest.findViewById(R.id.recyclerView);
    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setAdapter(new RecyclerView.Adapter() {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edittext, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 20;
        }
    });
    bsTest.show();
}

BSTest.java

public class BSTest extends BottomSheetDialog {
    public BSTest(@NonNull Context context) {
        super(context);
    }

    private BSTest(@NonNull Context context, @StyleRes int theme) {
        super(context, theme);
    }

    private BSTest(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }
}

bottomsheet_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

item_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

【问题讨论】:

标签: android android-softkeyboard bottom-sheet


【解决方案1】:

将此 KeyboardUtil 用作新解决方案!

在 BottomSheetFragment 的 onCreateDialog 中使用它

KeyboardUtil(getActivity(), view);

片段使用

new KeyboardUtil(this, findViewById(R.id.fragment_container));

通过使用这个 Util 类

https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java

图片来源:迈克彭斯

【讨论】:

    【解决方案2】:

    看来编辑您的清单并将android:windowSoftInputMode="adjustResize" 添加到此活动的&lt;activity&gt; 标记应该可以解决您的问题。

    文档:https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

    【讨论】:

    • 不...不起作用。如果adjustResize 是原因,为什么第一个输入没有调整大小但第二个输入做了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多