【问题标题】:Is it possible to implement Exposed Dropdown Menus using an spinner是否可以使用微调器实现 Exposed Dropdown Menus
【发布时间】:2019-10-31 04:52:23
【问题描述】:

Material 设计有一个使用 AutoCompleteTextView 实现的公开下拉菜单,如下所示:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text">

  <AutoCompleteTextView
      android:id="@+id/filled_exposed_dropdown"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

但我需要具有相同的外观和感觉,但具有微调器行为(没有自动完成,只需显示弹出窗口并选择一个项目)。

我禁用了 AutoCompleteTextView 上的编辑以避免使用自动完成功能,但是在选择其中一项后,自动完成功能只会列出与所选项目文本匹配的项目,给定此视图中使用的过滤器。这是代码:

    <AutoCompleteTextView
            android:id="@+id/filled_exposed_dropdown"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="16dp"
            **android:editable="false"**/>

我还设置了一个监听器,以便在单击时打开项目列表

val editTextFilledExposedDropdown = findViewById<AutoCompleteTextView>(R.id.filled_exposed_dropdown)
    editTextFilledExposedDropdown.setAdapter(adapter)
    editTextFilledExposedDropdown.setOnClickListener {
        (it as AutoCompleteTextView).showDropDown()
}

所以,我想知道是否可以使用微调器来实现这一点

这是我使用微调器的尝试,但它无法正确显示样式OutlinedBox.Dense.ExposedDropdownMenu,并且还显示了两个箭头底部图标,我认为一个用于样式,另一个用于微调器。

这是带有微调器的代码:

<com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint">
    <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layout_id"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Currency">
    <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/my_spinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</com.google.android.material.textfield.TextInputLayout>

【问题讨论】:

    标签: android material-design android-view autocompletetextview


    【解决方案1】:

    这就是我一直在寻找的,只需通过覆盖 getFilter 方法来禁用 AutoCompleteTextView 的自动完成功能。 答案贴在这里 (AutoCompleteTextView - disable filtering)

    【讨论】:

      【解决方案2】:

      更新:

      1行代码即可实现:

      android:editable="false" 
      

      AutoCompleteTextView

      来自 material.io 文档:

      注意:为了使菜单具有不可编辑的变体,您 应该禁用 AutoCompleteTextView 中的用户输入。那可以是 通过设置 android:editable="false" 在 自动完成文本视图。

      不知道为什么 Google 选择为此使用已弃用的属性...

      tools:ignore="Deprecated" 可以用来消除警告

      【讨论】:

      • 这没有给出正确的行为。作者提到了这一点:“我禁用了 AutoCompleteTextView 上的编辑以避免使用自动完成功能,但在选择其中一项后,自动完成功能只会列出与在此视图中使用的过滤器选择的项文本匹配的项。”
      • 应该使用 inputMode="none" 而不是 editable="false"
      • inputMode="none" 甚至不是一个真实的东西!? android:inputType="none" 是,但它仍然允许用户编辑 AutoCompleteTextView
      【解决方案3】:

      您可以在AutoCompleteTextView 上使用android:inputType="none"

      【讨论】:

        【解决方案4】:

        如何创建PopupMenu 而不是AutoCompleteTextView 以下是如何在 android 中创建菜单的示例:

            Context wrapper = new ContextThemeWrapper(getContext(), R.style.popupMenuStyle);
        //yourView is the view that you want to display the menu above it.
                        PopupMenu popupMenu = new PopupMenu(wrapper, yourView);
                        popupMenu.getMenuInflater().inflate(R.menu.menu, popupMenu.getMenu());
                        popupMenu.setOnMenuItemClickListener(item -> {
                            switch (item.getItemId()) {
                                case R.id.delete:
                                    //delete
                                    break;
                                case R.id.edit:
                                    //edit
                                    break;
                            }
                            popupMenu.dismiss();
                            return true;
                        });
                        popupMenu.show();
        

        menu.xml menu 文件夹内的文件:

        <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/edit"
                android:title="@string/edit">
        
            </item>
            <item
                android:id="@+id/delete"
                android:title="@string/delete">
            </item>
        </menu>
        

        【讨论】:

        • 这是一个好主意,但不是我想要的,我想要显示的项目将被动态加载,这取决于其他用户输入。
        • 好的,你没有在你的问题中提到这一点,但你可以这样做......看到这个答案stackoverflow.com/a/17312050/8660721
        猜你喜欢
        • 1970-01-01
        • 2016-04-07
        • 2015-01-02
        • 2017-08-08
        • 2023-03-31
        • 2014-05-04
        • 1970-01-01
        • 1970-01-01
        • 2021-12-18
        相关资源
        最近更新 更多