Android中CheckBox与CompoundButton源码解析
經歷過了前面一系列的講解,下面我們直接來看看系統里面的CheckBox與CompoundButton類的源碼文件。你肯定會發現很多熟悉的地方。
結合下面源碼,我們對它們進行解析解析,它里面使用的就是自定義drawable state。
我們首先直接看CheckBox的源碼
public class CheckBox extends CompoundButton {public CheckBox(Context context) {this(context, null);}public CheckBox(Context context, AttributeSet attrs) {this(context, attrs, com.android.internal.R.attr.checkboxStyle);}public CheckBox(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic void onInitializeAccessibilityEvent(AccessibilityEvent event) {super.onInitializeAccessibilityEvent(event);event.setClassName(CheckBox.class.getName());}@Overridepublic void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {super.onInitializeAccessibilityNodeInfo(info);info.setClassName(CheckBox.class.getName());} }我們可以看到,它里面并沒有做什么,因為它的操作都是繼承自CompoundButton.
我們先來看看下面一個普通的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="CheckBox"/></RelativeLayout>在上面的這個相對布局中,就寫了一個CheckBox,我們什么都不做,直接運行代碼,就會看到下面的運行界面:
當我們單擊這個CheckBox,我們發現他會被選中,那么它內部到底是怎么實現的呢?
其實使用過drawable state的人應該對這個并不陌生,我們經常這樣做:
1、在res/drawable文件下創建selector.xml,示例代碼如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="false"android:drawable="@drawable/title_button_back"></item><item android:state_pressed="true"android:drawable="@drawable/title_button_back_h"></item><item android:state_window_focused="false"android:drawable="@drawable/title_button_back"></item> </selector>2、編寫布局文件,為布局文件中的ImageButton設置selector,示例代碼如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent"> <ImageButton android:id="@+id/title_IB"android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="#00000000" android:layout_marginRight="4dp" android:layout_centerVertical="true" android:src="@drawable/selector"></ImageButton> </RelativeLayout>當我們單擊這個圖片按鈕的時候,圖片按鈕的圖片就會發生變化,其實CheckBox也是這樣做的,只是這些系統為我們做了。
下面我們來看看系統實現源碼:
上面在布局文件中直接寫了一個CheckBox,布局文件被解析后就會實例化這個CheckBox對象,就會執行CheckBox的構造函數:
從上面我們可以知道兩點:
1、CheckBox的默認樣式是com.android.internal.R.attr.checkboxStyle
2、最終執行的時候CompoundButton的構造函數
在frameworks/base/core/res/res/values/themes.xml文件中,已經初始化了checkBoxStyle樣式:
<item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item>在frameworks/base/core/res/res/values/styles.xml文件,我們來看看它的樣式是什么:
<style name="Widget.CompoundButton.CheckBox"><item name="android:button">?android:attr/listChoiceIndicatorMultiple</item> </style>在frameworks/base/core/res/res/values/themes.xml文件:
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>可以看到,CheckBox的默認樣式就是給它的button屬性賦值了一個btn_check,我們來看看btn_check文件里面的具體內容。
在frameworks/base/core/res/res/drawable/btn_check.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- Enabled states --><item android:state_checked="true" android:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/btn_check_on" /><item android:state_checked="false" android:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/btn_check_off" /><item android:state_checked="true" android:state_pressed="true"android:state_enabled="true"android:drawable="@drawable/btn_check_on_pressed" /><item android:state_checked="false" android:state_pressed="true"android:state_enabled="true"android:drawable="@drawable/btn_check_off_pressed" /><item android:state_checked="true" android:state_focused="true"android:state_enabled="true"android:drawable="@drawable/btn_check_on_selected" /><item android:state_checked="false" android:state_focused="true"android:state_enabled="true"android:drawable="@drawable/btn_check_off_selected" /><item android:state_checked="false"android:state_enabled="true"android:drawable="@drawable/btn_check_off" /><item android:state_checked="true"android:state_enabled="true"android:drawable="@drawable/btn_check_on" /><!-- Disabled states --><item android:state_checked="true" android:state_window_focused="false"android:drawable="@drawable/btn_check_on_disable" /><item android:state_checked="false" android:state_window_focused="false"android:drawable="@drawable/btn_check_off_disable" /><item android:state_checked="true" android:state_focused="true"android:drawable="@drawable/btn_check_on_disable_focused" /><item android:state_checked="false" android:state_focused="true"android:drawable="@drawable/btn_check_off_disable_focused" /><item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" /><item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" /></selector>我們看到,state_checked為true的時候,drawable=”@drawable/btn_check_on”,state_checked為false的時候,drawable=”@drawable/btn_check_off”.
@drawable/btn_check_on圖片和@drawable/btn_check_off圖片,在frameworks/base/core/res/res/drawable我們可以找到:
我們可以看到,其實就是根據不同的狀態,為button屬性賦值了不同的圖片資源,這就是我們看到的效果。
state_checked這個狀態的定義,在下面進行了定義:
<declare-styleable name="DrawableStates"><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when a view has input focus. --><attr name="state_focused" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when a view's window has input focus. --><attr name="state_window_focused" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when a view is enabled. --><attr name="state_enabled" format="boolean" /><!-- State identifier indicating that the object <var>may</var> display a check mark.See {@link R.attr#state_checked} for the identifier that indicates whether it isactually checked. --><attr name="state_checkable" format="boolean"/><!-- State identifier indicating that the object is currently checked. See{@link R.attr#state_checkable} for an additional identifier that can indicate ifany object may ever display a check, regardless of whether state_checked iscurrently set. --><attr name="state_checked" format="boolean"/><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when a view (or one of its parents) is currently selected. --><attr name="state_selected" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when the user is pressing down in a view. --><attr name="state_pressed" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when a view or its parent has been "activated" meaning the user has currentlymarked it as being of interest. This is an alternative representation ofstate_checked for when the state should be propagated down the view hierarchy. --><attr name="state_activated" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable}.--><attr name="state_active" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable}.--><attr name="state_single" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable}.--><attr name="state_first" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable}.--><attr name="state_middle" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable}.--><attr name="state_last" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},indicating that the Drawable is in a view that is hardware accelerated.This means that the device can at least render a full-screen scaledbitmap with one layer of text and bitmaps composited on top of itat 60fps. When this is set, the colorBackgroundCacheHint will beignored even if it specifies a solid color, since that optimizationis not needed. --><attr name="state_accelerated" format="boolean" /><!-- State value for {@link android.graphics.drawable.StateListDrawable StateListDrawable},set when a pointer is hovering over the view. --><attr name="state_hovered" format="boolean" /><!-- State for {@link android.graphics.drawable.StateListDrawable StateListDrawable}indicating that the Drawable is in a view that is capable of accepting a drop ofthe content currently being manipulated in a drag-and-drop operation. --><attr name="state_drag_can_accept" format="boolean" /><!-- State for {@link android.graphics.drawable.StateListDrawable StateListDrawable}indicating that a drag operation (for which the Drawable's view is a valid recipient)is currently positioned over the Drawable. --><attr name="state_drag_hovered" format="boolean" /><!-- State for {@link android.graphics.drawable.StateListDrawable StateListDrawable}indicating that a View has accessibility focus. --><attr name="state_accessibility_focused" format="boolean" /> </declare-styleable>我們可以看到<attr name="state_checked" format="boolean"/>
下面我們看看CompoundButton的構造函數,因為CheckBox最終執行的是它的構造函數。
public CompoundButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);TypedArray a =context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);if (d != null) {setButtonDrawable(d);}boolean checked = a.getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);setChecked(checked);a.recycle(); }我們可以知道CompoundButton自定義了兩個屬性,進入frameworks/base/core/res/res/values/attrs.xml文件:
<declare-styleable name="CompoundButton"><!-- Indicates the initial checked state of this button. --><attr name="checked" format="boolean" /><!-- Drawable used for the button graphic (e.g. checkbox, radio button, etc). --><attr name="button" format="reference"/> </declare-styleable>我們可以看到它定義了兩個屬性:checked,button,一個存放的是一個boolean類型的值,表示是否被選中,一個存放的是引用類型,對應的是一個圖片。
在構造函數中,我們獲取到了這兩個屬性值。上面就是對button這個屬性進行了默認賦值,然后我們這里就可以獲取到上面的btn_check這個xml文件的drawable。
也就是說Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button)中的d的到的就是btn_check這個xml文件的drawable。
然后分別調用了setButtonDrawable(d)和setChecked(checked)來對我們自定義的控件進行了設置。
我們來看看setButtonDrawable(d)函數:
public void setButtonDrawable(Drawable d) {if (d != null) {if (mButtonDrawable != null) {mButtonDrawable.setCallback(null);unscheduleDrawable(mButtonDrawable);}//這里設置Callback的原因就是refreshDrawableState刷新的時候可以回調到invalidateDrawable//這樣就可以重繪,這個我們在#詳解refreshDrawableList()的執行流程#這篇文件講過d.setCallback(this);d.setVisible(getVisibility() == VISIBLE, false);mButtonDrawable = d;setMinHeight(mButtonDrawable.getIntrinsicHeight());}refreshDrawableState(); }最后調用了refreshDrawableState這個方法,這個執行過程我們也分析過,整個執行思路都是一樣,不過不同的是,里面的很多方法都被覆蓋了。
這里我們再來把整個思路走一走。
refreshDrawableState執行的還是View里面的這個方法,直接看源碼。
public void refreshDrawableState() {//首先把標志設置為PFLAG_DRAWABLE_STATE_DIRTYmPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;drawableStateChanged();ViewParent parent = mParent;if (parent != null) {parent.childDrawableStateChanged(this);} }drawableStateChanged這個方法不在執行View里面的這個方法,因為CompoundButton里面有這個方法,我們看看CompoundButton的這個方法。
@Override protected void drawableStateChanged() {super.drawableStateChanged();if (mButtonDrawable != null) {int[] myDrawableState = getDrawableState();// Set the state of the DrawablemButtonDrawable.setState(myDrawableState);invalidate();} }getDrawableState這個函數還是調用的View的這個函數。
public final int[] getDrawableState() {//PFLAG_DRAWABLE_STATE_DIRTY是前面refreshDrawableState設置的//如果mDrawableState不為空,并且不需要刷新狀態,則直接返回,否則重新進行獲取if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {return mDrawableState;} else {//這里就是重新得到視圖狀態,并將其返回mDrawableState = onCreateDrawableState(0);//清除前面refreshDrawableState設置的標識mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;return mDrawableState;} }下面調用到了onCreateDrawableState這個方法,這個方法也不在是執行View里面的這個方法,因為CompoundButton里面也有這個方法。
@Override protected int[] onCreateDrawableState(int extraSpace) {final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);if (isChecked()) {mergeDrawableStates(drawableState, CHECKED_STATE_SET);}return drawableState; }結合之前我們詳解refreshDrawableList()的執行流程的分析。這個方法就是在之前的基礎上加入了一個判斷,如果isChekced為真,就把我們自定義的這個狀態加進去,這樣當前狀態里面就有我們的自定義的狀態,后面在狀態二維數組中查詢的時候,就可以找到對應的drawable圖片進行設置了。
這個函數執行完了就會回到上面的getDrawableState函數,然后把當前的狀態返回到drawableStateChanged函數中,接著看里面的代碼。
接著執行mButtonDrawable.setState(myDrawableState),把得到的當前狀態數組設置進去。里面的執行過去前面詳解refreshDrawableList()的執行流程的分析也說過,是完全一樣的,我們就省略,不明白就再看看,最后會執行invalidateSelf函數。
這個函數就會得到前面我們設置的Callback回調,調用它的invalidateDrawable方法。
public void invalidateSelf() {final Callback callback = getCallback();if (callback != null) {callback.invalidateDrawable(this);} }跟前面一樣,因為View實現了這個回調,所以它執行的是View里面的實現方法。在這個方法里面執行了invalidate函數,所以會執行onDraw方法.
在CompoundButton里面實現了這個方法,它不再直接執行View里面的這個方法了。
這樣這個圖片就繪制出來了,從btn_check里面我們可以看到最開始顯示的圖片是btn_check_off,當我們單擊這個CheckBox的時候,會執行performClick函數。
@Override public boolean performClick() {/** XXX: These are tiny, need some surrounding 'expanded touch area',* which will need to be implemented in Button if we only override* performClick()*//* When clicked, toggle the state */toggle();return super.performClick(); }接著執行toggle函數。
public void toggle() {setChecked(!mChecked); }這里面就把當前的狀態設置為與之前相反的狀態,剛開始為false,這個時候就為true.
接著我們看看setChecked函數。
public void setChecked(boolean checked) {if (mChecked != checked) {mChecked = checked;refreshDrawableState();notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);// Avoid infinite recursions if setChecked() is called from a listenerif (mBroadcasting) {return;}mBroadcasting = true;if (mOnCheckedChangeListener != null) {mOnCheckedChangeListener.onCheckedChanged(this, mChecked);}if (mOnCheckedChangeWidgetListener != null) {mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);}mBroadcasting = false; } }在這個里面又執行了refreshDrawableState函數,同樣把上面的refreshDrawableState過程又執行了一遍,執行在到onCreateDrawableState就不一樣了。
我們在來看看這個函數:
第一次執行的時候isChecked為false,自定義的這個狀態沒有合進去,這次就把自定義的這個狀態合進去,這樣就可以查詢到我們的狀態,所以就可以找到定義的drawable,所以圖片發生改變為btn_check_on。
最后把完整的CompoundButton源碼貼出來,可以對照上面將的,然后再根據前面講的詳解refreshDrawableList()的執行流程這個過程理解理解。
/** Copyright (C) 2007 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.widget;import com.android.internal.R;import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; import android.view.Gravity; import android.view.ViewDebug; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo;/*** <p>* A button with two states, checked and unchecked. When the button is pressed* or clicked, the state changes automatically.* </p>** <p><strong>XML attributes</strong></p>* <p>* See {@link android.R.styleable#CompoundButton* CompoundButton Attributes}, {@link android.R.styleable#Button Button* Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link* android.R.styleable#View View Attributes}* </p>*/ public abstract class CompoundButton extends Button implements Checkable {private boolean mChecked;private int mButtonResource;private boolean mBroadcasting;private Drawable mButtonDrawable;private OnCheckedChangeListener mOnCheckedChangeListener;private OnCheckedChangeListener mOnCheckedChangeWidgetListener;private static final int[] CHECKED_STATE_SET = {R.attr.state_checked};public CompoundButton(Context context) {this(context, null);}public CompoundButton(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CompoundButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);TypedArray a =context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);if (d != null) {setButtonDrawable(d);}boolean checked = a.getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);setChecked(checked);a.recycle();}public void toggle() {setChecked(!mChecked);}@Overridepublic boolean performClick() {/** XXX: These are tiny, need some surrounding 'expanded touch area',* which will need to be implemented in Button if we only override* performClick()*//* When clicked, toggle the state */toggle();return super.performClick();}@ViewDebug.ExportedPropertypublic boolean isChecked() {return mChecked;}/*** <p>Changes the checked state of this button.</p>** @param checked true to check the button, false to uncheck it*/public void setChecked(boolean checked) {if (mChecked != checked) {mChecked = checked;refreshDrawableState();notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);// Avoid infinite recursions if setChecked() is called from a listenerif (mBroadcasting) {return;}mBroadcasting = true;if (mOnCheckedChangeListener != null) {mOnCheckedChangeListener.onCheckedChanged(this, mChecked);}if (mOnCheckedChangeWidgetListener != null) {mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);}mBroadcasting = false; }}/*** Register a callback to be invoked when the checked state of this button* changes.** @param listener the callback to call on checked state change*/public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {mOnCheckedChangeListener = listener;}/*** Register a callback to be invoked when the checked state of this button* changes. This callback is used for internal purpose only.** @param listener the callback to call on checked state change* @hide*/void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {mOnCheckedChangeWidgetListener = listener;}/*** Interface definition for a callback to be invoked when the checked state* of a compound button changed.*/public static interface OnCheckedChangeListener {/*** Called when the checked state of a compound button has changed.** @param buttonView The compound button view whose state has changed.* @param isChecked The new checked state of buttonView.*/void onCheckedChanged(CompoundButton buttonView, boolean isChecked);}/*** Set the background to a given Drawable, identified by its resource id.** @param resid the resource id of the drawable to use as the background */public void setButtonDrawable(int resid) {if (resid != 0 && resid == mButtonResource) {return;}mButtonResource = resid;Drawable d = null;if (mButtonResource != 0) {d = getResources().getDrawable(mButtonResource);}setButtonDrawable(d);}/*** Set the background to a given Drawable** @param d The Drawable to use as the background*/public void setButtonDrawable(Drawable d) {if (d != null) {if (mButtonDrawable != null) {mButtonDrawable.setCallback(null);unscheduleDrawable(mButtonDrawable);}d.setCallback(this);d.setVisible(getVisibility() == VISIBLE, false);mButtonDrawable = d;setMinHeight(mButtonDrawable.getIntrinsicHeight());}refreshDrawableState();}@Overridepublic void onInitializeAccessibilityEvent(AccessibilityEvent event) {super.onInitializeAccessibilityEvent(event);event.setClassName(CompoundButton.class.getName());event.setChecked(mChecked);}@Overridepublic void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {super.onInitializeAccessibilityNodeInfo(info);info.setClassName(CompoundButton.class.getName());info.setCheckable(true);info.setChecked(mChecked);}@Overridepublic int getCompoundPaddingLeft() {int padding = super.getCompoundPaddingLeft();if (!isLayoutRtl()) {final Drawable buttonDrawable = mButtonDrawable;if (buttonDrawable != null) {padding += buttonDrawable.getIntrinsicWidth();}}return padding;}@Overridepublic int getCompoundPaddingRight() {int padding = super.getCompoundPaddingRight();if (isLayoutRtl()) {final Drawable buttonDrawable = mButtonDrawable;if (buttonDrawable != null) {padding += buttonDrawable.getIntrinsicWidth();}}return padding;}/*** @hide*/@Overridepublic int getHorizontalOffsetForDrawables() {final Drawable buttonDrawable = mButtonDrawable;return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);final Drawable buttonDrawable = mButtonDrawable;if (buttonDrawable != null) {final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;final int drawableHeight = buttonDrawable.getIntrinsicHeight();final int drawableWidth = buttonDrawable.getIntrinsicWidth();int top = 0;switch (verticalGravity) {case Gravity.BOTTOM:top = getHeight() - drawableHeight;break;case Gravity.CENTER_VERTICAL:top = (getHeight() - drawableHeight) / 2;break;}int bottom = top + drawableHeight;int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;int right = isLayoutRtl() ? getWidth() : drawableWidth;buttonDrawable.setBounds(left, top, right, bottom);buttonDrawable.draw(canvas);}}@Overrideprotected int[] onCreateDrawableState(int extraSpace) {final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);if (isChecked()) {mergeDrawableStates(drawableState, CHECKED_STATE_SET);}return drawableState;}@Overrideprotected void drawableStateChanged() {super.drawableStateChanged();if (mButtonDrawable != null) {int[] myDrawableState = getDrawableState();// Set the state of the DrawablemButtonDrawable.setState(myDrawableState);invalidate();}}@Overrideprotected boolean verifyDrawable(Drawable who) {return super.verifyDrawable(who) || who == mButtonDrawable;}@Overridepublic void jumpDrawablesToCurrentState() {super.jumpDrawablesToCurrentState();if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();}static class SavedState extends BaseSavedState {boolean checked;/*** Constructor called from {@link CompoundButton#onSaveInstanceState()}*/SavedState(Parcelable superState) {super(superState);}/*** Constructor called from {@link #CREATOR}*/private SavedState(Parcel in) {super(in);checked = (Boolean)in.readValue(null);}@Overridepublic void writeToParcel(Parcel out, int flags) {super.writeToParcel(out, flags);out.writeValue(checked);}@Overridepublic String toString() {return "CompoundButton.SavedState{"+ Integer.toHexString(System.identityHashCode(this))+ " checked=" + checked + "}";}public static final Parcelable.Creator<SavedState> CREATOR= new Parcelable.Creator<SavedState>() {public SavedState createFromParcel(Parcel in) {return new SavedState(in);}public SavedState[] newArray(int size) {return new SavedState[size];}};}@Overridepublic Parcelable onSaveInstanceState() {// Force our ancestor class to save its statesetFreezesText(true);Parcelable superState = super.onSaveInstanceState();SavedState ss = new SavedState(superState);ss.checked = isChecked();return ss;}@Overridepublic void onRestoreInstanceState(Parcelable state) {SavedState ss = (SavedState) state;super.onRestoreInstanceState(ss.getSuperState());setChecked(ss.checked);requestLayout();} }總結
以上是生活随笔為你收集整理的Android中CheckBox与CompoundButton源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: commandname
- 下一篇: commandname+commanda