ToggleButtonpublic class ToggleButton extends CompoundButton Displays checked/unchecked states as a button
with a "light" indicator and by default accompanied with the text "ON" or "OFF".
See the Toggle Buttons
guide. |
Fields Summary |
---|
private CharSequence | mTextOn | private CharSequence | mTextOff | private android.graphics.drawable.Drawable | mIndicatorDrawable | private static final int | NO_ALPHA | private float | mDisabledAlpha |
Constructors Summary |
---|
public ToggleButton(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.ToggleButton, defStyleAttr, defStyleRes);
mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn);
mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff);
mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f);
syncTextState();
a.recycle();
| public ToggleButton(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)
this(context, attrs, defStyleAttr, 0);
| public ToggleButton(android.content.Context context, android.util.AttributeSet attrs)
this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
| public ToggleButton(android.content.Context context)
this(context, null);
|
Methods Summary |
---|
protected void | drawableStateChanged()
super.drawableStateChanged();
if (mIndicatorDrawable != null) {
mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
}
| public java.lang.CharSequence | getTextOff()Returns the text for when the button is not in the checked state.
return mTextOff;
| public java.lang.CharSequence | getTextOn()Returns the text for when the button is in the checked state.
return mTextOn;
| protected void | onFinishInflate()
super.onFinishInflate();
updateReferenceToIndicatorDrawable(getBackground());
| public void | onInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)
super.onInitializeAccessibilityEvent(event);
event.setClassName(ToggleButton.class.getName());
| public void | onInitializeAccessibilityNodeInfo(android.view.accessibility.AccessibilityNodeInfo info)
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(ToggleButton.class.getName());
| public void | setBackgroundDrawable(android.graphics.drawable.Drawable d)
super.setBackgroundDrawable(d);
updateReferenceToIndicatorDrawable(d);
| public void | setChecked(boolean checked)
super.setChecked(checked);
syncTextState();
| public void | setTextOff(java.lang.CharSequence textOff)Sets the text for when the button is not in the checked state.
mTextOff = textOff;
| public void | setTextOn(java.lang.CharSequence textOn)Sets the text for when the button is in the checked state.
mTextOn = textOn;
| private void | syncTextState()
boolean checked = isChecked();
if (checked && mTextOn != null) {
setText(mTextOn);
} else if (!checked && mTextOff != null) {
setText(mTextOff);
}
| private void | updateReferenceToIndicatorDrawable(android.graphics.drawable.Drawable backgroundDrawable)
if (backgroundDrawable instanceof LayerDrawable) {
LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable;
mIndicatorDrawable =
layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle);
} else {
mIndicatorDrawable = null;
}
|
|