2013-03-31 47 views
0

我正在使用接口OnCheckedChangeListeneronCheckedChanged當我點擊checbox時會觸發該方法。Android從OnCheckedChangeListener的CompoundButton獲取CheckBox鍵名

但我有10個複選框。我如何獲得CompoundButton中點擊複選框的名稱。我認爲CompoundButton擁有所有necesarry值,但我不知道如何得到這個值

例如:

<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" /> 

如何讓現在的關鍵名稱作爲CompoundButtonnm_mute_all_sounds

 @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
buttonView. 

或者您如何知道我點擊了哪一個?

+0

我看不到屬性Android這樣的:對於複選框或任何其他 – Pragnani

+0

CompoundButton擁有所有我覺得值鍵,但我不能找到它 – senzacionale

回答

1

您可以直接使用該ID:

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

「或者你怎麼知道我點擊了哪一個?「最好使用這些ID。由於它們已編譯,所以如果ID更改,代碼將中斷,迫使開發人員更新ID。使用字符串鍵更脆弱。 – dmon