如何设置“暴露的下拉菜单”的初始值?

时间:2019-05-27 06:14:30

标签: android material-design

无法设置新引入的公开下拉菜单的起始值。

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"

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

2 个答案:

答案 0 :(得分:1)

设置适配器方法后,请尝试以下

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

答案 1 :(得分:0)

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