【问题标题】:How to set Exposed Dropdown Menus' start value?如何设置公开下拉菜单的起始值?
【发布时间】:2020-02-17 19:08:03
【问题描述】:

未能设置新引入的暴露下拉菜单的起始值。

https://github.com/material-components/material-components-android/blob/master/docs/components/Menu.md#exposed-dropdown-menus

我正在尝试获取第一个值集,但是当我尝试使用“selectedItemPosition”时出现错误,即未找到这些属性。

另外,尝试使用 java 代码实现这一点,给了我一个错误,即没有索引。

// Trying to set it via java code
Binding.dropdownSex.setAdapter(getAdapter(getResources().getStringArray(R.array.fragment_me_spinner_sex)));
        mBinding.dropdownSex.setSelection(1);   // java.lang.IndexOutOfBoundsException: setSpan (1 ... 1) ends beyond length 0 


// just a small method to get the adapter
private ArrayAdapter<String> getAdapter(String[] elements) {
        return new ArrayAdapter<>(Objects.requireNonNull(getContext()),
                R.layout.dropdown_menu_popup_item,
                elements);
    }

还有……

<!-- attribute android:selectedItemPosition not found. -->
 <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="32dp"
                android:paddingEnd="32dp"
                >

                <AutoCompleteTextView
                    android:id="@+id/dropdown_sex"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/sex"
                    android:focusable="false"
                    android:cursorVisible="false"

有人知道如何解决这个问题吗?

【问题讨论】:

  • 您找到解决方案了吗?我也面临这个问题。

标签: android material-design


【解决方案1】:

设置适配器方法后试试下面

dropdown_sex.setText(loadCenterList.get(mViewFlipper.displayedChild - 1), false)

【讨论】:

    【解决方案2】:

    使用mBinding.dropdownSex.setText(*TEXT OF ARRAY ITEM HERE*);

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 2012-10-11
      • 2021-07-10
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多