在 Spinner 更改时加载不同的 strings.xml 资源
当在微调器中选择新项目时,我试图更改我的活动的语言(只是活动而不是整个应用程序)。微调器包含代表我想要切换的语言的文本(法语、英语等)。
在我的 res 文件夹中,我在正确的文件夹中为每种语言都有单独的 strings.xml,即法语的 value-fr。在更改微调项目时,如何从对应的正确 strings.xml 加载?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<String> languages = new ArrayList<String>();
languages.add("English");
languages.add("French");
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, languages));
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int position, long arg3) {
Log.v("Value", "Language Value: " + position);
if(position == 0){
//Change text to English
}
else{
//Change text to French
}
}
I am am trying to change the language of my activity (just the activity not the whole app) when a new item is selected within a spinner. The spinner contains Text representing the language I would like to switch through (French, English etc.)
In my res folder I have separate strings.xml for each language in the correct folders i.e. values-fr for French. How can I, on the changing of the spinner item, load from the correct strings.xml which corresponds?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<String> languages = new ArrayList<String>();
languages.add("English");
languages.add("French");
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, languages));
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int position, long arg3) {
Log.v("Value", "Language Value: " + position);
if(position == 0){
//Change text to English
}
else{
//Change text to French
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 AndroidManifest.xml 中的 Activity 标记中
因此,您的 onItemSelected() 方法应如下所示:
我的答案基于:
如何在 Android 应用程序中强制使用语言
更改应用程序本身内的区域设置
in AndroidManifest.xml in activity tag
So your onItemSelected() method should look like this:
My answer is based on:
how to force language in android application
Changing Locale within the app itself