Custom a CheckedTextView in android

2019-04-15 19:23发布

问题:

I'm using a code that is not mine in my work. This code uses CheckedTextView, do this:

...
    AlertDialog.Builder alert;
        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        switch (soundPreference.getType()) {
        case BOOLEAN:
            CheckedTextView checkedTextView = (CheckedTextView) v;
            boolean checked = !checkedTextView.isChecked();
            ((CheckedTextView) v).setChecked(checked);
            switch (soundPreference.getKey()) {
            case SOUND_ACTIVE:

                sound.setAlarmActive(checked);
                break;
...

works perfectly, the problem is that I want I want to change the checkbox graphic. Right now it looks like (defualt, I think):

and I want to get something like this:

if the CheckedTextView were defined in xml, know how to do it. My problem is not being defined in xml, do not know how to do it.

I found this: Customize the CheckedTextView for ListView but I do not know how to implement it

appreciate any help.

Thank you very much in advance

回答1:

http://developer.android.com/reference/android/widget/CheckedTextView.html#setCheckMarkDrawable%28int%29

Example:

myCheckedTextView.setCheckMarkDrawable(R.drawable.icon);

This method allows you to set the check mark image dynamically. You should call it when setting up the CheckedTextView. The method in the developer reference just needs the id of the image resource you want to use.

There is also a version that takes a Drawable as a parameter instead of an id.



回答2:

You need to create a custom list_item layout.

For full explanation go through the below link...

http://amitandroid.blogspot.in/2013/09/android-custom-multiple-choice.html

Hope this will help you.