Setting Android CheckBox to a different image… and then back to the original images

余生长醉 提交于 2019-12-22 13:49:18

问题


I'm using the following (very common) code to change the checkbox image in my Android app.

    mCheck = (CheckBox) findViewById(R.id.chkMine);
    mCheck.setButtonDrawable(R.drawable.my_image);

I see many people asking for that. But I never see the second part:

How do I put BACK the original checkbox imagery, later in my code?

I hesitate to try to design all my own images (checked, unchecked, ghosted-checked, ghosted-unchecked, etc) because I need the original images that would normally appear on many different version of Android.

Maybe, initially save the default image with a (non-existing?) getButtonDrawable() call, and then reuse it later?

I would think it would be something as simple as calling setButtonDrawable() a 2nd time to "undo" my changes. Or is it?

Thanks.


回答1:


As you already correctly mention yourself, unfortunately there is no getButtonDrawable() to get a reference to the drawable used before you replace it. Obviously you could copy Android's resources over to your local project and use those to reset the CheckBox's button, but that would mean you would have to take into account all the different styles from themes, let alone any changes device manufacturers might make to those resources. It's not impossible to go down this lane, but you'll find out it will be quite of a hassle for something that sounds to simple.

What you may want to consider is doing the following: query the resources for resource ids of the drawables that you need. That way you don't explicitly have to deal with different themes, as the lookups will do that. You can easily put this functionality in a dedicated method with just a few lines. Example:

private static int getDefaultCheckBoxButtonDrawableResourceId(Context context) {
    // pre-Honeycomb has a different way of setting the CheckBox button drawable
    if (Build.VERSION.SDK_INT <= 10) return Resources.getSystem().getIdentifier("btn_check", "drawable", "android");
    // starting with Honeycomb, retrieve the theme-based indicator as CheckBox button drawable
    TypedValue value = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple, value, true);
    return value.resourceId;
}

Below a quick example of setting the button drawable to some custom image, and reset it afterwards. I'm simply toggling between the app icon and original button drawable whenever the check state changes.

CheckBox mCheckBox = (CheckBox) findViewById(R.id.checkbox);
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        button.setButtonDrawable(isChecked ? R.drawable.icon : getDefaultCheckBoxButtonDrawableResourceId(StackOverflowActivity.this));
    }
});



回答2:


I think to retrieve the original images a call to setButtonDrawable() should work again.
But instead of your resource you'll reference to android's original resource.

mCheck = (CheckBox) findViewById(R.id.chkMine);
mCheck.setButtonDrawable(android.R.drawable.*);

Probably you've to lookup the file name on your own:

platforms > android-* > data > res > drawable-*
(source: accepted answer)

edit
Ha! Me knew me've seen that site before: Android R Drawables ^^v
(credits to Mef)



来源:https://stackoverflow.com/questions/10235237/setting-android-checkbox-to-a-different-image-and-then-back-to-the-original-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!