Custom a CheckedTextView in android

放肆的年华 提交于 2019-12-22 00:56:16

问题


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.



来源:https://stackoverflow.com/questions/18878928/custom-a-checkedtextview-in-android

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