旋转后微调器值消失

1dkrff03  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(262)

我有一个简单的“材质微调器”实现:

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

    <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>

以及填充列表的片段oncreate方法

AutoCompleteTextView editTextFilledExposedDropdown;
ArrayAdapter<String> adapter = null;

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    String[] type = new String[] {"Bed-sitter", "Single", "1- Bedroom", "2- Bedroom","3- Bedroom"};
    adapter =new ArrayAdapter<>( getContext(),R.layout.dropdown_menu_popup_item, type);
    editTextFilledExposedDropdown = view.findViewById(R.id.filled_exposed_dropdown);
    editTextFilledExposedDropdown.setAdapter(adapter);
}

只要我不选择物品并旋转手机,代码就可以正常工作。一旦我转动手机 onCreate 方法再次调用,但由于某种原因,唯一可用的项是我之前选择的项。该菜单不显示所有其他项目。
原因可能是什么?我故意重新填充arrayadapter,只是为了确保在旋转手机时使用相同的对象。

fdbelqdn

fdbelqdn1#

使用自定义视图。这是materialdesign类中的一个bug,尚未得到及时修复:

public class ExposedDropdownMenu extends MaterialAutoCompleteTextView {

    public ExposedDropdownMenu(@NonNull @NotNull Context context) {
        super(context);
    }
    public ExposedDropdownMenu(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public ExposedDropdownMenu(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attributeSet, int defStyleAttr) {
        super(context, attributeSet, defStyleAttr);
    }

    @Override
    public boolean getFreezesText() {
        return false;
    }
}
cgvd09ve

cgvd09ve2#

在onresume()方法中设置适配器,它将正常工作。我是通过手机回复的,所以无法向您显示代码片段,而且我没有足够的声誉发表评论,所以发布为回复。

相关问题