如何在android:tag中添加许多值 [英] How to add many values in android:tag

查看:205
本文介绍了如何在android:tag中添加许多值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android XML中,我想在android:tag中添加多个值.我找到了一种方法,可以使用多种方法来添加值.

In Android XML, I want to add multiple values in android:tag. I found one way that we can use multiple for adding values.

<TextView   
         android:id="@+id/myTextView"   
         android:layout_width="wrap_content"   
         android:layout_height="wrap_content">
        <tag android:id="@+id/value_one" android:value="@string/value_one"/>
        <tag android:id="@+id/value_two" android:value="@string/value_two"/>
        <tag android:id="@+id/value_three" android:value="@string/value_three"/>
 </TextView>

但是此方法仅在API级别21及更高版本中有效.使用上述方法时,Android提供以下错误.

But this method work only in API level 21 and above. The Android provides the following error while using above method.

标记仅在21级及更高级别的API中使用(当前最小值为19)

tag is only used in API level 21 and higher (current min is 19)

在API级别19中,我们可以选择以编程方式设置标签.还有其他选择可以在XML本身中添加多个字符串或对象吗?

In API level 19, we have an option to set tag in programmatically. Is there any other option to add multiple strings or Object in XML itself?

推荐答案

在android:tag ="中设置多个值的诀窍不在android:tag ="之内,因为您始终可以像下面这样设置多个标签: android:tag ="TAG1:TAG2:TAG3".诀窍是在代码中查找并解析标记.在检查是否存在特定标记时,请执行以下操作(调整并根据需要添加错误处理):

The trick to set multiple values inside android:tag="" does not lie within android:tag="" because you can always set multiple tags as in: android:tag="TAG1:TAG2:TAG3". The trick is in finding and parsing the tag in your code. When you are checking for the existence of a particular tag, do something like the following (tweak and add error handling as necessary):

public static boolean isTagDefined(View view, String tag) {
    boolean rc = false;
    Object tags = view.getTag();
    if (tags instanceof String) {
        rc = ((String) tags).contains(tag);
    }
    return rc;
}

命名和定界标签时要小心.如果需要键/值对,请更改标签和解析技术(StringTokenizer)以处理诸如"KEY1_VALUE:KEY2_VALUE"之类的内容.您也可以使用JSON对象,但我认为这可能会过大,因为简单的控制/调试功能可能需要使用视图标签.

Be careful when naming and delimiting tags. If you need key/value pairs, change your tag and parsing technique (StringTokenizer) to handle something like "KEY1_VALUE:KEY2_VALUE". You could also use JSON objects but I believe that would be overkill because the use of view tags is likely to be needed for simple control/debug functions.

这篇关于如何在android:tag中添加许多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆