簡體   English   中英

AutoCompleteTextView (setText) 和 fragment.detach().attach()

[英]AutoCompleteTextView (setText) and fragment.detach().attach()

我制作應用程序並添加此 function 更改語言。 我從使用的 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>

我究竟做錯了什么。 對不起我的英語不好

我沒有考慮 AutoCompleteTextView 根據輸入的值過濾列表的內容。 有必要斷開適配器中的過濾器。 如果過濾器沒有被禁用,那么當片段被重繪時,它只會在適配器中顯示那些滿足過濾條件的結果。 AutoCompleteTextView - 禁用過濾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM