简体   繁体   中英

Android set android:state_checked=“false” in code

I want to create a selector using the code below:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/sh_radio_icon_checked" />
    <item android:state_checked="false" android:drawable="@drawable/sh_radio_icon_unchecked" />
</selector>

So far I have managed to add the first item, like this:

StateListDrawable drawable = new StateListDrawable();
int[] sFocusedSelected = {android.R.attr.state_checked};
Drawable dFocusedSelected = getResources().getDrawable(R.drawable.sh_radio_icon_checked);
drawable.addState(sFocusedSelected, dFocusedSelected);

But do I add the state_checked=false since there's no state_unchecked constant?

Observe "-" (Minus/Hyphen) in the beginning of "android.R.attr.state_checked"

sld.addState(new int[] {-android.R.attr.state_checked  },  greyD);

Android : How to update the selector(StateListDrawable) programmatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM