在Android

时间:2017-02-01 11:48:28

标签: java android

我可以将我的应用程序更改为波斯语,但每当我关闭应用程序并重新打开它时,它都会恢复为默认的英语。我面临的另一个问题是,当我将语言改回英语时,它并没有改变!在我点击它之后我改变了语言后又回到了英语。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        Configuration config = getBaseContext().getResources().getConfiguration();

        final String lang = settings.getString("LANG", "");
        if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
            Locale locale = new Locale(lang);
            Locale.setDefault(locale);
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources()
                    .getDisplayMetrics());
        }

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setPrompt("select language");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, languages);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2 ,long arg3 ) {
                Configuration config = new Configuration();
                Locale locale;
                switch (arg2) {
                    case 0:
                        config.locale = Locale.ENGLISH;
                        break;
                    case 1:
                    PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
                                .edit().putString("LANG", "fa").apply();
                        config.locale = new Locale("fa");
                        startActivity(new Intent(getBaseContext(), MainActivity.class));
                        finish();
                        break;
                }
                getResources().updateConfiguration(config, null);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
    }
    private String[] languages = { "English","فارسی" };
}

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);


    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    settings = PreferenceManager.getDefaultSharedPreferences(this);
    if(settings.contains("LANG"))
    {
        if (settings.getString("LANG", "").equals("fa"))
        {
            String languageToLoad = "fa";
            Locale locale = new Locale(languageToLoad);
            //Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(
                config, getBaseContext().getResources().getDisplayMetrics());
        }
        else
        {
            String languageToLoad = "en";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(
                config, getBaseContext().getResources().getDisplayMetrics());
        }
    }


    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    spinner.setPrompt("select language");


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, languages);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {


        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2 ,long arg3 )
        {
            switch (arg2)
            {
            case 0:
                settings.edit().putString("LANG", "en").apply();
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();

                break;

            case 1:
                settings.edit().putString("LANG", "fa").apply();
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();

                break;

            }

        }
    }

答案 1 :(得分:0)

在onCreate检查中选择最后一种语言。

      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
      settings = PreferenceManager.getDefaultSharedPreferences(this);


        if (settings.getString("LANG", "").equals("fa"))
         {
            changeLanguage("fa");
            else
             {
            changeLanguage("en");
         }


     Spinner spinner = (Spinner) findViewById(R.id.spinner1);
     spinner.setPrompt("select language");


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
                               int arg2 ,long arg3 )
    {
        Configuration config = new Configuration();
        Locale locale;
        switch (arg2)
        {
        case 0:

             changeLanguage("en");
            break;
        case 1:

            changeLanguage("fa");

            break;

        }

    }


  private void changeLanguage(String lng)
  {
     String languageToLoad = "en";

    if(lng.quals("fa"))
    {
      languageToLoad=lng;
        Locale locale = new Locale(languageToLoad);
        //Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(
            config, getBaseContext().getResources().getDisplayMetrics());

       settings.edit().putString("LANG", languageToLoad).apply();
            startActivity(new Intent(getBaseContext(), MainActivity.class));
            finish();
    }
  }