24

我目前正在使用 Material Design TextInputLayout OutlinedBox 如下图所示:

        <android.support.design.widget.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/myEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Title"
                android:inputType="text"/>

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

我正在尝试Spinner在我的 TextInputEditText 下添加一个下拉框,并希望保持相同的样式:OutlinedBox。

我看到 Material Design, Material Design Text Fields似乎支持下拉菜单。如此处所示的区域:

如此处所示的区域

我目前正在使用 Spinner 生成下拉菜单。

        <Spinner
            style="@style/Widget.AppCompat.Spinner.DropDown"
            android:id="@+id/option"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:dropDownWidth="match_parent" />

似乎不可能在 OutlinedBox 设计之后添加下拉菜单。是否有一个库可以让我实现这一点,或者有没有更好的方法在 Material Design 中实现它?

4

11 回答 11

28

我假设你想在里面有一个暴露的下拉菜单TextInputLayout我有同样的问题,你可以做的是AutoCompleteTextView在你TextInputLayout的内部使用,如下所示XML。这是我如何处理这个问题的一个例子。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingRight="30dp"
            android:paddingEnd="30dp"
            tools:ignore="RtlSymmetry"
            android:layout_margin="5dp">

            <ImageView
                android:layout_width="30dp"
                android:layout_margin="10dp"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/ic_location_city_black_24dp"
                android:layout_gravity="center"
                />




        <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="Type"
            android:orientation="horizontal"
            >



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

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


        </LinearLayout>

    </LinearLayout>

您还需要一个项目布局资源来填充下拉弹出窗口。下面的示例提供了一个遵循 Material Design 指南的布局。

res/layout/dropdown_menu_popup_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:textAppearance="?attr/textAppearanceSubtitle1"/>

根据您的需要,在您的课程中添加以下代码。

String[] type = new String[] {"Bed-sitter", "Single", "1- Bedroom", "2- Bedroom","3- Bedroom"};

        ArrayAdapter<String> adapter =
                new ArrayAdapter<>(
                        this,
                        R.layout.dropdown_menu_popup_item,
                        type);

        AutoCompleteTextView editTextFilledExposedDropdown =
                findViewById(R.id.filled_exposed_dropdown);
        editTextFilledExposedDropdown.setAdapter(adapter);

如果这无济于事,请检查材料设计页面中的 Exposed Dropdown Menus。[ https://material.io/develop/android/components/menu/][1]

这是我关于堆栈溢出的第一个答案,希望对您有所帮助。

于 2020-04-19T15:57:47.173 回答
14

只需使用TextInputLayout包含在材质组件库中的样式Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu.

就像是:

  <com.google.android.material.textfield.TextInputLayout
      style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
      android:hint="Hint text"
      ...>

    <AutoCompleteTextView
        android:id="@+id/outlined_exposed_dropdown_editable"
        .../>
  </com.google.android.material.textfield.TextInputLayout>

在此处输入图像描述

于 2019-09-11T07:45:59.257 回答
10

我相信这个文件根本没有显示Spinner。我认为它显示的是TextInputLayout带有下拉图标的。

解剖部分的图标小节中,它说

5. 下拉图标

下拉箭头表示文本字段具有嵌套的选择组件。

现在,你如何提供“嵌套选择组件”我不确定......

于 2018-11-07T23:12:29.690 回答
5

从其他答案中,“AutoCompleteTextView”是答案,但它与微调器的作用不同。

这是我的解决方案。只需将普通的 edittext 放入 TextInputLayout 并禁用此 editText 输入。并放置一个 0dp,0dp 微调器用于正常微调器工作。

不要让微调器可见性=消失,因为如果它消失了,微调器侦听器将不起作用

布局.xml

  <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_10dp"
        android:theme="@style/ThemeOverlay.App.TextInputLayout">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/arrow_down_pacific_blue"
            android:focusable="false"
            android:hint="şehir"
            android:inputType="none" />

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

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:spinnerMode="dialog"
        tools:listitem="@layout/general_spinner_item" />

爪哇代码

将单击侦听器设置为 edittext 以触发微调器单击

findViewById(R.id.editText).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    spinner.performClick();
                }
            });

在微调器侦听器中,从所选项目设置edittext文本,

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                selectedCity= (City) parent.getAdapter().getItem(position);

                editText.setText(selectedCity.getScreenText());

                RDALogger.debug("selectedObject " + selectedCity);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

和结果视图

在此处输入图像描述

于 2021-01-26T13:22:31.377 回答
4

我正在使用以下材料库来获取微调器

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'

这是我的布局

 <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:hint="@string/select_wifi"
            app:hintTextAppearance="@style/hintStyle"
            app:startIconDrawable="@drawable/wifi">

            <com.google.android.material.textfield.MaterialAutoCompleteTextView
                style="@style/textInputEdittext"
                android:inputType="none" />

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

查看此图像以了解微调器

在此处输入图像描述

于 2020-12-07T21:10:05.020 回答
4

似乎他们实际上使用了一个包裹 AutoCompleteTextView 的 TextInputLayout。请注意,它们已经是材料组件主题 [ https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md]

请参阅: https ://material.io/design/components/menus.html#exposed-dropdown-menu https://material.io/develop/android/components/menu/

于 2019-06-06T18:46:23.683 回答
4

我用这个解决了我的问题:在XML中:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layout"
        style="@style/AppTheme.ExposedDropdownMenu"                        
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <AutoCompleteTextView
            android:id="@+id/my_spinner_dropdown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>

在代码中:

        layout.keyListener=null
        ArrayAdapter(it, android.R.layout.simple_list_item_1, list).also { 
         adapter ->
            layout.setAdapter(adapter)
        }

致谢:如何使 EditText 在 Android 中无法通过 XML 进行编辑?

于 2020-05-25T19:23:19.547 回答
3

我知道现在回答这个问题为时已晚,但像我这样陷入此类或类似问题的人可能会发现这非常有用。根据要求访问此回购它的完美解决方案。该库支持所有材质 TextInputLayout 样式。感谢“Mamoon Al-hawamdeh”为这个令人惊叹的图书馆。

在此处输入图像描述

于 2021-06-01T11:32:57.043 回答
2

您可以查看我的Medium 文章,其中我介绍了一个自定义 MaterialSpinner,它支持双向数据绑定和选择跟踪。生成的布局看起来像这样简单:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/input_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/selection_hint"
    app:startIconDrawable="@drawable/selection_icon"
    app:boxBackgroundMode="outline"
    app:endIconMode="@{viewModel.items == null || viewModel.items.size() != 0 ? TextInputLayout.END_ICON_DROPDOWN_MENU : TextInputLayout.END_ICON_NONE}">

    <com.example.MaterialSpinner
        android:id="@+id/items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:items="@{viewModel.items}"
        app:selectedPosition="@={viewModel.selectedItemPosition}"
        app:emptyText="@string/selection_no_item_text" />

</com.google.android.material.textfield.TextInputLayout>
于 2021-04-21T15:25:16.920 回答
0

所有这些答案都很有帮助,但重要的一点是,如果您设置app:endIconMode属性,您的下拉菜单将不起作用。

于 2021-07-14T10:51:49.287 回答
0

只需inputType从“文本”更改为“无”

<android.support.design.widget.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/myEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Title"
            android:inputType="none"/>

    </android.support.design.widget.TextInputLayout>
于 2021-11-18T05:27:19.673 回答