首先,我在LanguageActivity.java中遇到了一些问题。可以说,如果我从微调器中选择“ zh”,则应该将strings.xml更改为“ zh”。但是我发现界面中的某些字符串没有更改为[zh] strings.xml。

java - 多语言应用程序问题-LMLPHP

这是我的LanguageActivity.java代码

             @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_language);
    spinnerctrl = (Spinner) findViewById(R.id.spinner1);
    spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {

            if (pos == 1) {

                Toast.makeText(parent.getContext(),
                        "You have selected English", Toast.LENGTH_SHORT)
                        .show();
                setLocale("en");
            } else if (pos == 2) {

                Toast.makeText(parent.getContext(),
                        "You have selected ", Toast.LENGTH_SHORT)
                        .show();
                setLocale("zh");
            } else if (pos == 3) {

                Toast.makeText(parent.getContext(),
                        "You have selected Hindi", Toast.LENGTH_SHORT)
                        .show();
                setLocale("hi");
            }
            else if (pos == 4) {

                Toast.makeText(parent.getContext(),
                        "You have selected Malay", Toast.LENGTH_SHORT)
                        .show();
                setLocale("ms");
            }

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Nothing to select", Toast.LENGTH_LONG).show();
        }

    });
}

public void setLocale(String lang) {
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    if (!conf.locale.getLanguage().equals(lang)) {
        conf.locale = new Locale(lang);
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, LanguageActivity.class);
        startActivity(refresh);
        finish();
    }
}

最佳答案

要应用Locale更改,您必须重新启动应用程序。

尝试这个:

Intent i = getBaseContext().getPackageManager()
             .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);


希望能帮助到你。

10-08 08:49