如何在Android MaterialButtonToggleGroup中检查按钮位置

如何在Android MaterialButtonToggleGroup中检查按钮位置,android,material-design,android-button,android-togglebutton,materialbutton,Android,Material Design,Android Button,Android Togglebutton,Materialbutton,我在xml中用2MaterialButton定义了一个MaterialButton-togglegroup,如下所示: 我找到了getCheckedButtonIds()方法(返回Id列表)和OnButtonCheckedListener(返回整数Id),但是get(int index)方法返回索引位置的视图,而不是具有特定Id的视图 如何(以编程方式)在给定时刻获取选中按钮的视图? 有一种方法返回当前选中的位置(或索引)因为您使用的是app:singleSelection=“true”您可

我在xml中用2
MaterialButton
定义了一个
MaterialButton-togglegroup
,如下所示:


我找到了
getCheckedButtonIds()
方法(返回Id列表)和
OnButtonCheckedListener
(返回整数Id),但是
get(int index)
方法返回索引位置的视图,而不是具有特定Id的视图

如何(以编程方式)在给定时刻获取选中按钮的视图?
有一种方法返回当前选中的位置(或索引)

因为您使用的是
app:singleSelection=“true”
您可以使用
getCheckedButtonId()
方法来获取此组中所选
MaterialButton
的唯一id。
您可以使用以下内容:

MaterialButtonToggleGroup materialButtonToggleGroup = 
         findViewById(R.id.toggle_button_group);
int buttonId = materialButtonToggleGroup.getCheckedButtonId();
MaterialButton button = materialButtonToggleGroup.findViewById(buttonId);
您还可以使用
getCheckedButtonIds()
并在ID上循环

List<Integer> ids = materialButtonToggleGroup.getCheckedButtonIds();
for (Integer id:ids){
      MaterialButton materialButton = materialButtonToggleGroup.findViewById(id);
      //....
}
List id=materialButtonToggleGroup.getCheckedButtonId();
用于(整数id:ids){
MaterialButton MaterialButton=materialButtonToggleGroup.findViewById(id);
//....
}

由于您使用的是
app:singleSelection=“true”
您可以使用
getcheckedbutonid()
方法获取此组中所选
MaterialButton
的唯一id。
您可以使用以下内容:

MaterialButtonToggleGroup materialButtonToggleGroup = 
         findViewById(R.id.toggle_button_group);
int buttonId = materialButtonToggleGroup.getCheckedButtonId();
MaterialButton button = materialButtonToggleGroup.findViewById(buttonId);
您还可以使用
getCheckedButtonIds()
并在ID上循环

List<Integer> ids = materialButtonToggleGroup.getCheckedButtonIds();
for (Integer id:ids){
      MaterialButton materialButton = materialButtonToggleGroup.findViewById(id);
      //....
}
List id=materialButtonToggleGroup.getCheckedButtonId();
用于(整数id:ids){
MaterialButton MaterialButton=materialButtonToggleGroup.findViewById(id);
//....
}

是的,我认为这是目前最好的解决方案。但是Android没有提供一个返回选定索引列表的方法来使用get(index)方法获取选定视图,这似乎很奇怪。@capo11
materialbuttonogglegroup
不能使用索引(作为数组)。所有的方法(
check
uncheck
示例)都适用于ID视图。是的,我知道。目前只有一种方法可以处理索引:
get(index:Int)
。这个方法对我非常有用,因为它返回一个视图。我想详细地操作这个视图,但我必须实现一个循环,以确定idex属于所选视图。我本来希望找到另一种类似这样的方法:
get(id:Int)
returnview。方法
get(index)
ViewGroup
继承,对于
materialbuttonogroup
是的,我认为这是目前最好的解决方案。但是Android没有提供一个返回选定索引列表的方法来使用get(index)方法获取选定视图,这似乎很奇怪。@capo11
materialbuttonogglegroup
不能使用索引(作为数组)。所有的方法(
check
uncheck
示例)都适用于ID视图。是的,我知道。目前只有一种方法可以处理索引:
get(index:Int)
。这个方法对我非常有用,因为它返回一个视图。我想详细地操作这个视图,但我必须实现一个循环,以确定idex属于所选视图。我本来希望找到另一种类似这样的方法:
get(id:Int)
returnview。方法
get(index)
ViewGroup继承,不特定于
materialbuttonogroup