CheckedTextViewpublic abstract class CheckedTextView extends TextView implements CheckableAn extension to TextView that supports the {@link android.widget.Checkable} interface.
This is useful when used in a {@link android.widget.ListView ListView} where the it's
{@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to
something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}. |
Fields Summary |
---|
private boolean | mChecked | private int | mCheckMarkResource | private android.graphics.drawable.Drawable | mCheckMarkDrawable | private int | mBasePaddingRight | private int | mCheckMarkWidth | private static final int[] | CHECKED_STATE_SET |
Constructors Summary |
---|
public CheckedTextView(android.content.Context context)
this(context, null);
| public CheckedTextView(android.content.Context context, android.util.AttributeSet attrs)
this(context, attrs, 0);
| public CheckedTextView(android.content.Context context, android.util.AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CheckedTextView, defStyle, 0);
Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
if (d != null) {
setCheckMarkDrawable(d);
}
boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
setChecked(checked);
a.recycle();
|
Methods Summary |
---|
protected void | drawableStateChanged()
super.drawableStateChanged();
if (mCheckMarkDrawable != null) {
int[] myDrawableState = getDrawableState();
// Set the state of the Drawable
mCheckMarkDrawable.setState(myDrawableState);
invalidate();
}
| public boolean | isChecked()
return mChecked;
| protected int[] | onCreateDrawableState(int extraSpace)
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
| protected void | onDraw(android.graphics.Canvas canvas)
super.onDraw(canvas);
final Drawable checkMarkDrawable = mCheckMarkDrawable;
if (checkMarkDrawable != null) {
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int height = checkMarkDrawable.getIntrinsicHeight();
int y = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
int right = getWidth();
checkMarkDrawable.setBounds(
right - mCheckMarkWidth - mBasePaddingRight,
y,
right - mBasePaddingRight,
y + height);
checkMarkDrawable.draw(canvas);
}
| public void | setCheckMarkDrawable(int resid)Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn
when {@link #isChecked()} is true.
if (resid != 0 && resid == mCheckMarkResource) {
return;
}
mCheckMarkResource = resid;
Drawable d = null;
if (mCheckMarkResource != 0) {
d = getResources().getDrawable(mCheckMarkResource);
}
setCheckMarkDrawable(d);
| public void | setCheckMarkDrawable(android.graphics.drawable.Drawable d)Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.
if (d != null) {
if (mCheckMarkDrawable != null) {
mCheckMarkDrawable.setCallback(null);
unscheduleDrawable(mCheckMarkDrawable);
}
d.setCallback(this);
d.setVisible(getVisibility() == VISIBLE, false);
d.setState(CHECKED_STATE_SET);
setMinHeight(d.getIntrinsicHeight());
mCheckMarkWidth = d.getIntrinsicWidth();
mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
d.setState(getDrawableState());
mCheckMarkDrawable = d;
} else {
mPaddingRight = mBasePaddingRight;
}
requestLayout();
| public void | setChecked(boolean checked)Changes the checked state of this text view.
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
}
| public void | setPadding(int left, int top, int right, int bottom)
super.setPadding(left, top, right, bottom);
mBasePaddingRight = mPaddingRight;
| public void | toggle()
setChecked(!mChecked);
|
|