如何在没有微调器的情况下显示下拉列表?

时间:2021-05-28 09:15:56

标签: android kotlin

嗨,

我有一个问题,我不知道如何解决它。

所以首先,我将 TextInputLayout 与 AutoCompleteText 视图一起使用,以显示向下的样式,并且效果很好,但是当我旋转屏幕或配置更改(例如暗模式/亮模式切换)时,autcompleteTextView 不会显示下拉菜单。我知道这是一个悬而未决的问题,但我没有找到可行的解决方案。

我厌倦了 setFreezesText = false,但是当我旋转屏幕时它起作用了,并且在配置更改时,任何其他情况下它都不起作用。

layout:
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/opportunity_recording_hr_person_auto_complete_container"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/responsible_hr_person_label">

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

fragment:
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        
        binding.opportunityRecordingHrPersonAutoCompleteContent.freezesText = false
    }

所以在那之后,我将其更改为简单的 AutoCompleteTextView,如果它获得焦点,则显示下拉列表,但不幸的是,它不起作用,因为当发生配置更改时,我收到此错误:无法添加窗口-- 令牌空无效;您的活动是否正在运行?

layout:
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name_of_project_label"
                        android:labelFor="@+id/opportunity_recording_customer_auto_complete_content"/>
                    <AutoCompleteTextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/opportunity_recording_customer_auto_complete_content"
                        android:inputType="text"/>

fragment:
  // I call this on onCiewCreated()
    private fun initListeners() {
        binding.opportunityRecordingCustomerAutoCompleteContent.setOnFocusChangeListener { _, isFocused ->
            if (isFocused)
                binding.opportunityRecordingCustomerAutoCompleteContent.showDropDown()
        }
}

有没有办法至少解决其中一个问题?

0 个答案:

没有答案