Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android从OnCheckedChangeListener的CompoundButton中获取复选框键名_Android - Fatal编程技术网

Android从OnCheckedChangeListener的CompoundButton中获取复选框键名

Android从OnCheckedChangeListener的CompoundButton中获取复选框键名,android,Android,我正在使用interfaceoncheckedchangedlistener,当我单击checbox时,onCheckedChanged方法被激发 但我有10个复选框。如何在CompoundButton中获取已单击复选框的名称。我认为CompoundButton具有所有必需的值,但我不知道如何获取该值 例如: <CheckBox android:id="@+id/cb_mute_all_sounds" andr

我正在使用interface
oncheckedchangedlistener
,当我单击checbox时,
onCheckedChanged
方法被激发

但我有10个复选框。如何在CompoundButton中获取已单击复选框的名称。我认为
CompoundButton
具有所有必需的值,但我不知道如何获取该值

例如:

<CheckBox
                    android:id="@+id/cb_mute_all_sounds"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:key="nm_mute_all_sounds" />

或者您如何知道我单击了哪一个?

您可以直接使用ID:

 public void onCheckedChangeListener(CompoundButton theView, boolean checked) {
   if (theView.getId() == R.id.nm_mute_all_sounds) {
     // do stuff
   }
 }

我没有看到像android:key这样的属性用于复选框或任何其他组合按钮具有我认为的所有值,但我找不到它“或者您如何知道我单击了哪个?”最好使用ID。由于它们是编译的,如果ID发生更改,代码将中断,迫使开发人员更新ID。使用字符串键要容易得多。
 public void onCheckedChangeListener(CompoundButton theView, boolean checked) {
   if (theView.getId() == R.id.nm_mute_all_sounds) {
     // do stuff
   }
 }