【问题标题】:AutoCompleteTextView (setText) and fragment.detach().attach()AutoCompleteTextView (setText) 和 fragment.detach().attach()
【发布时间】:2021-06-29 21:04:51
【问题描述】:

我制作应用程序并添加此功能更改语言。我从使用的 MainActivity 更改片段中的语言

...
        val locale = Locale("es")
        Locale.setDefault(locale)
        val configuration = Configuration()
        configuration.locale = locale
        this.resources.updateConfiguration(configuration, null)
...
 supportFragmentManager.beginTransaction().
           detach(scaleFragment).attach(scaleFragment).commit
... 

在片段 CreateView 中

    ...
    val listSpeed = Scales.initSpeedsList().map{String.format("%.1f", it)}
  
    val adapterSpeed: ArrayAdapter<String> = ArrayAdapter(
            requireContext(),
            R.layout.list_item_scale,
            listSpeed)
    
    bindingScale.editSpeed.setAdapter(adapterSpeed)
    bindingScale.editSpeed.setText(bindingScale.editSpeed.adapter.getItem(1).toString(), false)//1
    ...

如果我注释掉第 1 行 (setText),则该字段为空,下拉列表由 5 行组成(应该是 5 行)。如果第 1 行未注释,则该字段被填充并且下拉列表为一行或根本没有。如果我不做 supportFragmentManager.beginTransaction().detach(scaleFragment).attach(scaleFragment).commit(即我没有将页面翻译成另一种语言),那么一切正常 - 该字段已填写并且 drop-下列表包含所有必要的选项

     <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/editSpeedLayout"
  

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_marginTop="4dp"
            android:hint="@string/speed_head"
            app:layout_constraintEnd_toStartOf="@id/editVoltageLayout"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textHeaderScale"
            app:suffixText="@string/mm_per_sec">
    
            <AutoCompleteTextView
                android:id="@+id/editSpeed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="none" />
        </com.google.android.material.textfield.TextInputLayout>

我做错了什么。对不起我的英语

【问题讨论】:

    标签: android kotlin fragment autocompletetextview


    【解决方案1】:

    我没有考虑 AutoCompleteTextView 根据输入的值过滤列表的内容。有必要断开适配器中的过滤器。如果过滤器没有被禁用,那么当片段被重绘时,它只会在适配器中显示那些满足过滤条件的结果。 AutoCompleteTextView - disable filtering

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2017-01-18
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多