Android - 具有字符串值的自定义状态

时间:2015-11-26 09:02:55

标签: android

我按照this site上的教程来实现state-list的自定义状态。它适用于布尔属性,但不适用于字符串属性。

这是我的源代码:

  • 州名单:

                       

  • 属性:

    <declare-styleable name="CategoryIconView">
            <attr name="category_id" format="string"/>
        </declare-styleable>
  • 自定义视图:

    public class CategoryIconView extends ImageView {
    
    // this is used when we want to merge our state with the ones from the system
    private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id};
    
    String categoryID;
    public CategoryIconView(Context context) {
        super(context);
    }
    
    public CategoryIconView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CategoryIconView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        LogUtils.d(getClass().getSimpleName(), "category ID " + categoryID);
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (categoryID != null) {
            mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET);
        }
        return drawableState;
    }
    
    // this method will be used when you will need to set your image button state in the code
    public void setCategoryID(String categoryId){
        this.categoryID = "Three";
        refreshDrawableState();
    }
    }
    

1 个答案:

答案 0 :(得分:0)

实际上你遗漏了一些陈述,下面找到了这个

的解决方案
public class CategoryIconView extends ImageView {

        // this is used when we want to merge our state with the ones from the system
        private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id};

        String categoryID;

        public CategoryIconView(Context context) {
            super(context);
        }

        public CategoryIconView(Context context, AttributeSet attrs) {
            super(context, attrs);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
        // from attributes.xml
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);

        // R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
        // custom state list in attributes.xml
        categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
        }

        public CategoryIconView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
            // from attributes.xml
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);

            // R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
            // custom state list in attributes.xml
            categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
        }

        @Override
        public int[] onCreateDrawableState(int extraSpace) {
            Log.d(getClass().getSimpleName(), "category ID " + categoryID);
            final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
            if (categoryID != null) {
                mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET);
            }
            return drawableState;
        }

        // this method will be used when you will need to set your image button state in the code
        public void setCategoryID(String categoryId) {
            this.categoryID = "Three";
            refreshDrawableState();
        }
    }