【问题标题】:Navigation after selecting an item in spinner [closed]在微调器中选择项目后导航[关闭]
【发布时间】:2022-01-23 04:51:33
【问题描述】:

我有一个问题,我有一个带有 4 个项目的微调器,当我选择例如项目编号 4 时,我想导航到另一个片段我如何在 android kotlin 中做到这一点??

【问题讨论】:

    标签: android kotlin android-spinner


    【解决方案1】:

    阅读https://developer.android.com/guide/topics/ui/controls/spinner

    Google Developers 的这篇文章将告诉您,您必须使用 AdaperView.OnItemSelectedListener 扩展您的活动,然后设置对微调器的引用以使 onItemSelectedListener 指向活动类的引用(“this”)。

    除此之外,它们还为您提供了两个必须设置才能使用的覆盖函数。添加扩展程序后,只需将它们添加到您的活动中即可。

    您想检查微调器上的项目位置是否等于所需的相同位置或字符串:

    override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id: Long) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    
      //Check for the current text, use this for every possible item you have
      //when statement can even be used for this
      if(parent.getItemAtPosition(pos).toString() == "Item1"){
           //Add code to switch the fragment
       }
    }
    
    override fun onNothingSelected(parent: AdapterView<*>) {
        // Another interface callback
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      相关资源
      最近更新 更多