gpt4 book ai didi

java - 如何通过选择微调项目来更改应用程序语言?

转载 作者:行者123 更新时间:2023-12-01 17:53:07 26 4
gpt4 key购买 nike

我想给 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;
}

}

谁能帮忙解决这个问题吗?

最佳答案

您需要以编程方式更改设备的区域设置,如下所示:

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(如果有必要)。

关于java - 如何通过选择微调项目来更改应用程序语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47787726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com