在android中,我们如何拥有条件资源值?

在android中,我们如何拥有条件资源值?,android,string,resources,Android,String,Resources,我需要访问字符串的条件资源值,如下所示: 字符串1 .. 如果(defaultVariableValue=val_1) 测试应用程序密钥 其他 实时应用程序密钥 ..… 在模块的build.gradle中使用产品口味: android { compileSdkVersion xx buildToolsVersion "xx.x.x" defaultConfig { minSdkVersion xx targetSdkVersion xx

我需要访问字符串的条件资源值,如下所示:


字符串1
..
如果(defaultVariableValue=val_1)
测试应用程序密钥
其他
实时应用程序密钥
..…



在模块的build.gradle中使用产品口味:

android {
    compileSdkVersion xx
    buildToolsVersion "xx.x.x"
    defaultConfig {
        minSdkVersion xx
        targetSdkVersion xx
        versionCode x
        versionName "xxxxxxxxx"
    }
    productFlavors {

        testing{
            applicationId = "com.example.testing"
            resValue "string", "resString", " Test Application Key "
        }

        live{
            applicationId = "com.example.live"
            resValue "string", "resString", " Live Application Key "
        }
    }
}

然后在代码中只需调用
getResources().getString(R.string.resString)并根据构建,它将为您提供测试密钥或live密钥。

@Swapnil:我不知道您的要求是什么。但您可以使用equals()代替=

if(getResources().getString(R.string.str1))
   getResources().getString(R.string.resString)
if (defaultVariableValue.equals(val_1))
{

// Your Staff
 .getResources().getString(R.string.resString);
}
else
{
// Your Staff
.getResources().getString(R.string.yyyyy);
}

但是,为什么live或test app的软件包名称会改变?在.java文件中处理这个条件为什么不添加两个字符串并将这个条件放在java代码中?你在使用gradle吗?我的意思是,我有两个API键,它们受用户区域的限制(比如亚洲/世界其他地区)。在应用程序中,当用户选择“ASIA”时,应用程序应读“Test Application Key”,否则读“Live app Key”,我希望它在资源文件中处理,而不是在适用的地方写if/ELSE。听起来很有希望,根据您对问题的回答,我想知道我是否有可能在places Application ID中放入运行时变量?这可能是我的特定案例“com.example.testing”/“com.example.live”的根解决方案-这些值在您的解决方案中可以是可变的吗?