【问题标题】:Why is the drop-down list for my AutoCompleteTextView obscured by the keyboard only for certain list widths?为什么我的 AutoCompleteTextView 的下拉列表仅在某些列表宽度下被键盘遮挡?
【发布时间】:2020-05-10 18:08:04
【问题描述】:

我的应用使用 AutoCompleteTextView 显示列表中的数据作为搜索的一部分,如下所示:

我希望搜索字段可见,因此我在 Fragment 中添加了以下内容:

activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

果然,当我触摸搜索文本字段时,它按预期工作:

但是,当我开始在文本字段中输入字符时,下拉列表会被键盘遮挡,除非我将 dropDownWidth 设置为 101 dp 或更小:

例如,当我将其设置为 102 dp 或 match_parent 时,我得到以下信息:

我可以滚动列表,但它仍然被遮挡。

我在 Stack Overflow 上搜索了该问题,但没有出现任何问题。顺便说一句,我正在使用 Android 10 的 Pixel 2 上运行,有趣的是,在运行 API 26 的模拟华为 8 上不会出现此问题。

【问题讨论】:

    标签: android android-fragments dropdown autocompletetextview


    【解决方案1】:

    我前段时间遇到过这个问题。贝娄是一个例子(java代码 - 因为它是前一段时间) 我使用了 DelayAutoCompleteTextView 并制作了自定义适配器

    私人无效 setAutocompleteForPatientName(ArrayList 患者) { contactName = (DelayAutoCompleteTextView) findViewById(R.id.nameET); 联系人姓名.setThreshold(1); contactName.setAdapter(new YourCustomAdapter(this, contacts)); 联系人名称.setLoadingIndicator( (android.widget.ProgressBar) findViewById(R.id.pb_loading_indicator)); contactName.setOnItemClickListener(新的 AdapterView.OnItemClickListener() { @覆盖 public void onItemClick(AdapterView adapterView, View view, int i, long l) {

                DO WHATEVER YOU WANT WITH SELECTION
    
            }
        });
    }
    

    我的适配器正在实现 Filterable 。

    下面是延迟自动完成

    公共类 DelayAutoCompleteTextView 扩展 AutoCompleteTextView {

    private static final int MESSAGE_TEXT_CHANGED = 100;
    private static final int DEFAULT_AUTOCOMPLETE_DELAY = 750;
    
    private int mAutoCompleteDelay = DEFAULT_AUTOCOMPLETE_DELAY;
    private ProgressBar mLoadingIndicator;
    
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            DelayAutoCompleteTextView.super.performFiltering((CharSequence) msg.obj, msg.arg1);
        }
    };
    
    public DelayAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public void setLoadingIndicator(ProgressBar progressBar) {
        mLoadingIndicator = progressBar;
    }
    
    public void setAutoCompleteDelay(int autoCompleteDelay) {
        mAutoCompleteDelay = autoCompleteDelay;
    }
    
    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
        if (mLoadingIndicator != null) {
            mLoadingIndicator.setVisibility(View.VISIBLE);
        }
        mHandler.removeMessages(MESSAGE_TEXT_CHANGED);
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_TEXT_CHANGED, text), mAutoCompleteDelay);
    }
    
    @Override
    public void onFilterComplete(int count) {
        if (mLoadingIndicator != null) {
            mLoadingIndicator.setVisibility(View.GONE);
        }
        super.onFilterComplete(count);
    }
    

    }

    这里是xml

                    <YOURPACKAGE THAT CONTAINS YOUR CUSTOM VIEW CLASS .DelayAutoCompleteTextView
                        android:id="@+id/nameET"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:hint="@string/patientName"
                        android:imeOptions="flagNoExtractUi|actionSearch"
                        android:inputType="textCapSentences"
                        android:paddingRight="20dp"
                        android:textSize="12sp" />
    
                    <ProgressBar
                        android:id="@+id/pb_loading_indicator"
    
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|right"
                        android:layout_marginRight="20dp"
                        android:visibility="gone" />
                </FrameLayout>
    

    【讨论】:

    • 感谢您的建议,Raluca,但我不喜欢使用任何第三方库。我已经将此作为错误提交给 Google。
    • 它不是第三方库,它是您创建的自定义类。无需导入模块,无需获取任何第三方
    • 做你在项目中添加的那个类。但它不是第三方它是自定义视图类
    • 谢谢,我试试看!如果它解决了我的问题,我会重新发放赏金并奖励给你。
    • 不幸的是,用您的自定义 DelayAutoCompleteTextView 替换 AutoCompleteTextView 没有效果。可能与我的应用使用的 Android X 库有冲突。
    【解决方案2】:

    与此同时,我通过将 AutoCompleteTextView 移到列表上方来解决该问题:

    现在列表显示得很好:

    我认为问题是由于 AutoCompleteTextView 位于页面底部。

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多