blob: ec791f4752ec42ba8761c41e64befb1db59eade0 [file] [log] [blame]
package com.xtremelabs.robolectric.shadows;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.CompoundButton;
import com.xtremelabs.robolectric.util.Implementation;
import com.xtremelabs.robolectric.util.Implements;
/**
* Shadows the {@code android.widget.CompoundButton} class.
* <p/>
* Keeps track of whether or not its "checked" state is set and deals with listeners in an appropriate way.
*/
@SuppressWarnings({"UnusedDeclaration"})
@Implements(CompoundButton.class)
public class ShadowCompoundButton extends ShadowTextView implements Checkable {
private boolean mChecked;
private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener;
public void __constructor__(Context context, AttributeSet attributeSet) {
super.__constructor__(context, attributeSet);
setChecked(this.attributeSet.getAttributeBooleanValue("android", "checked", false));
}
@Implementation
@Override public void toggle() {
setChecked(!mChecked);
}
@Implementation
@Override public boolean performClick() {
toggle();
return super.performClick();
}
@Implementation
@Override public boolean isChecked() {
return mChecked;
}
@Implementation
@Override public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged((CompoundButton) realView, mChecked);
}
}
}
@Implementation
public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
}