如何解决旋转后微调值消失
我*有一个简单的“材质微调器”实现:
<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 只是为了确保在旋转手机时我使用相同的对象。
解决方法
使用自定义视图。这是一个来自 materialdesign 类的错误,但尚未修复:
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;
}
}
,
在 onResume() 方法中设置适配器,它会正常工作。
@override
public void onResume(){
super.onResume()
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);l̥
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。