遍历AttributeSet枚举

| 我有以下代码:
    private static boolean hasTargetStyle(AttributeSet attributes) {
        final Enumeration<?> attributeNames = attributes.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            final Object attributeName = attributeNames.nextElement();
            if (attributeName.equals(MY_STYLE_NAME)) {
                return true;
            }
        }

        return false;
    }
现在,我认为这段代码将逐步遍历每个属性名称。但这只是给我其他所有属性名称(带有偶数索引的属性名称)。 这是怎么了?     
已邀请:

bab

我认为它没有索引-
Set
没有索引。代码看起来不错。除非
getAttributeNames()
返回错误实现的枚举,否则它应该起作用。     
我目前看不到您的代码有什么问题,但请尝试使用Collections.list
private static boolean hasTargetStyle(AttributeSet attributes) {
    final List<?> attributeNames = Collections.list(attributes.getAttributeNames());

    for(Object name: attributeNames){
        if(name.equals(MY_STYLE_NAME)){
            return true;
        }
    }

    return false;
}
    
我怀疑这是
java.util.Enumeration
的问题(尽管这只是一个接口,实际的实现可能会有错误)。您的实现对我来说很好。 其他想法:最初的“ 5”可能仅包含所有其他属性。尝试设置一个断点,并查看实际属性集的内部。     
我在调试器中查看的内部列表具有交替的名称和值。因此,我的代码在某种意义上是正确的,但我的意图是错误的。     

要回复问题请先登录注册