【问题标题】:Android Material ButtonToggleGroup get checkedButtonIdAndroid Material ButtonToggleGroup 获取checkedButtonId
【发布时间】:2020-04-19 15:41:44
【问题描述】:

在最后的版本中,按钮切换组支持 singleSelection 和 selectionRequired

 implementation 'com.google.android.material:material:1.2.0-alpha06'

 app:selectionRequired="true"
 app:singleSelection="true

效果很好,但是当我尝试获取已选择按钮的 id 时,返回 -1

var x = toggleGroup.checkedButtonId

如何始终检查按钮 ID? 我知道 -1 表示当前按钮已被选中。但我有几个 toggleGroup,我需要即时知道选择了哪些按钮 id。

【问题讨论】:

  • 该方法getCheckedButtonId()在组不处于单选模式时返回-1。
  • 我检查了我在 xml 中也有 app:singleSelection="true" 以及调试器和 isSingleSelection(),它们在代码运行中返回“true”。也许这个问题是因为我使用“视图绑定”

标签: android kotlin material-design


【解决方案1】:

为了解决这个问题,使用方法 addOnButtonCheckedListener 中的 isChecked 参数

    binding.toggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked ->

        if (isChecked){

           //You code here
       }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2019-01-09
    • 2020-02-28
    • 1970-01-01
    相关资源
    最近更新 更多