简体   繁体   English

如何在Android中的xml中为字符串资源指定空值?

[英]How to specify null value for a string resource in xml in Android?

In my values.xml, I have some string resources. 在我的values.xml中,我有一些字符串资源。 But sometimes, I want to have a null value for that string resource for specific reasons. 但有时,出于特定原因,我希望该字符串资源具有空值。

How do I specify that string to have a null value? 如何指定该字符串具有null值?

values.xml: values.xml:

<string name="default_item_0">Some String</string>
<string name="default_item_1">@null</string>

Java code: Java代码:

String item0 = context.getString(R.string.default_item_0);
String item1 = context.getString(R.string.default_item_1);

Expected: 预期:

String item0 = "Some String";
String item1 = null;

Actual: 实际:

String item0 = "Some String";
String item1 = "@0";

I tried the @null value, but it returns a string of @0 instead. 我尝试了@null值,但它返回一个@0的字符串。

Is it possible to have a null value in xml? 是否可以在xml中使用null值?

As you know resources exist because they're supposed to be "something NOT null" , and @null is simply a reference resource with a zero-value id . 如您所知,资源存在是因为它们应该是“非空的东西” ,而@null只是一个零值id参考资源

Is it possible to have a null value in xml? 是否可以在xml中使用空值?

Null value for string resources? 字符串资源的空值? No and honestly it doesn't seem reasonable 不,老实说这似乎不合理

But in this case with your specific reasons I suggest you to add an empty string to resources: 但在这种情况下,根据您的具体原因,我建议您向资源添加一个空字符串:

<string name="empty"/>

Then you can check if it is empty or not : 然后你可以检查它是否为空:

String str = getString(R.string.empty);
    if (TextUtils.isEmpty(str)) {
        //do something
    }

PS: Earlier I used this approach to map indices to the correct items of an array, as you guess in my case some indices were skipped so I had to skip some array items as well. PS:早些时候我使用这种方法将索引映射到数组的正确项目,因为你猜测在我的情况下一些索引被跳过,所以我不得不跳过一些数组项目。

You can try with matches 你可以试试matches

Tests whether this string matches the given regularExpression. 测试此字符串是否与给定的regularExpression匹配。 This method returns true only if the regular expression matches the entire input string. 仅当正则表达式与整个输入字符串匹配时,此方法才返回true。

if(getResources().getString(R.string.default_item_1).matches("@null")) 
{ 
     // Your logic
}
else
    {
     // Your logic
    }

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. 如果需要使用String.format(String,Object ...)格式化字符串,则可以通过将格式参数放在字符串资源中来实现。

Courtesy dynamic String using String.xml? 使用String.xml提供动态String?

You can use method for this. 您可以使用此方法。

 public static boolean isNullString(String string) {
    try {
        if (string.trim().equalsIgnoreCase("null") || string.trim() == null || string.trim().length() < 0 || string.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        return true;
    }
}

Use this method as 使用此方法

if(isNullStrind(your string value){
//true
write your code
}else{
//false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM