【问题标题】:How to change the app language by selecting a spinner item?如何通过选择微调器项目来更改应用程序语言?
【发布时间】:2017-12-13 07:34:12
【问题描述】:

我想给Spinner 更改应用程序的语言。我正在创建一个演示应用程序,其中我有多个 strings.xml 文件。我创建了一个Spinner 并添加了语言列表。

单击一种语言(微调项)我想更改应用程序的语言。

怎么做?

strings.xml

<string name="app_name">Multi Language App</string>
<string name="action_settings">Settings</string>

<string name="welcome">Welcome!</string>
<string name="email">Email Address</string>
<string name="password">Password</string>
<string name="login">Login</string>
<string name="signup">Don\'t have account? Sign Up</string>


<string-array name="languages">
    <item name="English"></item>
    <item name="French"></item>
    <item name="Hindi"></item>
    <item name="Japanese"></item>
</string-array>

strings.xml(de)

<string name="welcome">Willkommen!</string>
<string name="email">Email Addresse</string>
<string name="password">passowrd</string>
<string name="login">Login</string>
<string name="signup">müssen nicht angemeldet? Anmeldung</string>

strings.xml(fr)

<string name="welcome">accueil</string>
<string name="email">adresse e-mail</string>
<string name="password">mot de passe</string>
<string name="login">connexion</string>
<string name="signup">Ne pas avoir un compte? signer</string>

stringd.xml(hi)

<string name="welcome">स्वागतम</string>
<string name="email">ईमेल पता</string>
<string name="password">पासवर्ड</string>
<string name="login">लॉगिन</string>
<string name="signup">खाता नहीं है? साइन अप करें</string>

MainActivity.java

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {

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

        getActionBar().hide();

        Spinner spinner = (Spinner) findViewById(R.id.spinnerLanguage);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.languages, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);



    }
    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)

        if(pos == 0) {

           //what to be done here to chnage the app's language?

        }

    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

有人可以帮忙吗?

【问题讨论】:

标签: java android xml localization


【解决方案1】:

您需要像这样以编程方式更改设备的区域设置:

Locale locale = new Locale("hi"); // where 'hi' is Language code, set this as per your Spinner Item selected
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());

如有必要,请刷新您的 UI。

【讨论】:

  • 很高兴这对您有帮助:)
【解决方案2】:

首先在findViewById之后添加这一行。

Spinner spinner = (Spinner) findViewById(R.id.spinnerLanguage);
spinner.setOnItemSelectedListener(this);

然后在onItemSelected()中添加以下代码

String lang= parent.getItemAtPosition(pos).toString();
    String languageToLoad = null;

    if(lang.equals("Hindi")){
        languageToLoad="hi";
    }else if(lang.equals("English")){
        languageToLoad="en";
    }else if(lang.equals("French")){
        languageToLoad="fr";
    }else if(lang.equals("Japanese")){
        languageToLoad="de";
    }
    if(languageToLoad!=null) {
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
    }

希望对你有所帮助。

【讨论】:

    【解决方案3】:

    我觉得这个链接可以帮到你Localize the UI with Translations Editor

    请注意德语更好:)

    <string name="welcome">Willkommen!</string>
    <string name="email">Email Adresse</string>
    <string name="password">Password</string>
    <string name="login">Login</string>
    <string name="signup">Sie sind nicht angemeldet? Anmelden</string>
    

    【讨论】:

      【解决方案4】:

      调用(即):

      setLanguage(ApplicationHelper.Language.DE);
      

      [...]

      public void setLanguage(ApplicationHelper.Language l)
          {
              Intent intent = getIntent();
              overridePendingTransition(0, 0);
              intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
              finish();
              overridePendingTransition(0, 0);
              startActivity(intent);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-06
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多