1package com.xtremelabs.robolectric.shadows; 2 3import android.widget.Checkable; 4import android.widget.CompoundButton; 5import com.xtremelabs.robolectric.internal.Implementation; 6import com.xtremelabs.robolectric.internal.Implements; 7 8/** 9 * Shadows the {@code android.widget.CompoundButton} class. 10 * <p/> 11 * Keeps track of whether or not its "checked" state is set and deals with listeners in an appropriate way. 12 */ 13@SuppressWarnings({"UnusedDeclaration"}) 14@Implements(CompoundButton.class) 15public class ShadowCompoundButton extends ShadowTextView implements Checkable { 16 private boolean checked; 17 private CompoundButton.OnCheckedChangeListener onCheckedChangeListener; 18 19 @Override public void applyAttributes() { 20 super.applyAttributes(); 21 setChecked(this.attributeSet.getAttributeBooleanValue("android", "checked", false)); 22 } 23 24 @Implementation 25 @Override public void toggle() { 26 setChecked(!checked); 27 } 28 29 @Implementation 30 @Override public boolean performClick() { 31 toggle(); 32 return super.performClick(); 33 } 34 35 @Implementation 36 @Override public boolean isChecked() { 37 return checked; 38 } 39 40 @Implementation 41 @Override public void setChecked(boolean checked) { 42 if (this.checked != checked) { 43 this.checked = checked; 44 if (onCheckedChangeListener != null) { 45 onCheckedChangeListener.onCheckedChanged((CompoundButton) realView, this.checked); 46 } 47 } 48 } 49 50 @Implementation 51 public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) { 52 onCheckedChangeListener = listener; 53 } 54 55 public CompoundButton.OnCheckedChangeListener getOnCheckedChangeListener() { 56 return onCheckedChangeListener; 57 } 58} 59