溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

自定義 checkbox 新玩法 ?

發(fā)布時間:2020-06-11 23:03:29 來源:網(wǎng)絡(luò) 閱讀:382 作者:鄒君安 欄目:移動開發(fā)

自定義 checkbox 新玩法 ?

第一步:selector

編寫 drawable/selector_checkbox_voice.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_voice_off" android:state_checked="true"/>
    <item android:drawable="@drawable/ic_voice_on" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_voice_off"/>
</selector>
第二步:style

VoiceCheckboxTheme

<!--自定義的checkbox-->
<style name="VoiceCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/selector_checkbox_voice</item>
</style>
第三步:布局文件里

<CheckBox
    android:id="@+id/cb_voice"
     //這里使用
    android:layout_width="@dimen/dp21"
    android:layout_height="@dimen/dp28"
    android:gravity="center"
    android:layout_marginLeft="@dimen/dp30"
    />
第四步:效果

看左邊第二個

//點擊前
自定義 checkbox 新玩法 ?
//點擊后
自定義 checkbox 新玩法 ?

你可以監(jiān)聽狀態(tài):

//語音是否關(guān)閉
    mCb_Voice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                //執(zhí)行關(guān)閉語音
                MGToast.showToast("執(zhí)行關(guān)閉語音");
            }else {
                //執(zhí)行開啟語音
                MGToast.showToast("執(zhí)行開啟語音");
            }
        }
    });


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI