Android解讀CheckBox

來源:互聯網
上載者:User

標籤:android   abstract   public   super   null   

開啟源碼中CheckBox.java檔案,我們可以看到如下內容:

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);    }}

可以看到,CheckBox是繼承於CompoundButton,而CompoundButton:

public abstract class CompoundButton extends Button implements Checkable

是Button的一個子類,由此可知,CheckBox也為Button的一個子類。從上面的代碼中可以看出,CheckBox的不同之處就是添加了屬性checkboxStyle,我們在源碼中找到該樣式的定義如下:

<style name="Widget.CompoundButton.CheckBox">        <item name="android:background">@android:drawable/btn_check_label_background</item>        <item name="android:button">@android:drawable/btn_check</item>    </style>

其中,在資源檔中找到btn_check_label_background,該圖片是背景透明沒有內容的一張圖片,因此CheckBox的顯示就是由btn_check決定,我們開啟btn_check.xml檔案,可以看到以下內容:

<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>

每一個item就對應著CheckBox在不同的選擇狀態下顯示的效果,瞭解該檔案的內容後,我們就可以更具自己的需要來設計美觀的CheckBox。RadioButton的顯示與CheckBox基本相似,可以仿照CheckBox加以修改。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.