如何从特定的“值”文件夹中获取字符串?

时间:2012-09-19 12:48:35

标签: android

例如:我有一个多语言应用程序,我想从随机语言中获取一些字符串,用户必须猜测它是什么语言。 如何从其他语言中获取单个字符串?

3 个答案:

答案 0 :(得分:1)

应用程序使用的特定于语言的字符串取决于在android系统(Link)中设置的本地系统。因此,Android会自动显示正确的值。您无法硬编码要使用的特定(或随机)值。

完成任务的一种方法是以编程方式更改android的本地设置:

    Locale locale = new Locale("fr");
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, null);
    getResources().getString(R.string.local_string);

在这种情况下,唯一要做的就是随机播放本地标识符。当然,您应该在更改之前保护本地权限的实例,以便您可以将其重置为原始状态。祝你好运;)

修改
这是我写的一些脏代码来验证:

    Locale orgLocal = getResources().getConfiguration().locale;
    String[] availLocales = { "fr", "de", "en" };
    Locale tempLocale = new Locale(availLocales[new Random().nextInt(availLocales.length)]);
    Configuration config = new Configuration();
    config.locale = tempLocale;
    getResources().updateConfiguration(config, null);
    String randLangString = getResources().getString(R.string.local_string);
    config.locale = orgLocal;
    getResources().updateConfiguration(config, null);
    Toast.makeText(this, randLangString, Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

首先使用此代码更改语言设置

Locale locale = new Locale("kn");
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, null);
getResources().getString(R.string.local_string);

然后支持所有类型的语言 Unicode字体 - “ARIALUNI.TTF”Arial Unicode字体(复制到资产文件夹)

Typeface font= Typeface.createFromAsset(getAssets(), "ARIALUNI.TTF");  TextView tv1.setTypeface(font);
tv1.setText(R.string.txt);

* 从values-en-for中获取R.string.text值,为kannada语言获取value-kn文件夹*

答案 2 :(得分:0)

我们非常惊讶和相信我们只能通过改变配置来启用我们想要的语言。根据需要

Locale locale;
                  locale = new Locale("fr","FR");


        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        this.getActivity().getResources().updateConfiguration(config, this.getActivity().getResources().getDisplayMetrics());