How to change the language of android app from within?(如何从内部更改android应用程序的语言?)
问题描述
我正在开发一个漫画应用程序.我有三个单选按钮,分别是英语、法语和西班牙语.当用户单击其中任何一个时,来自 values-es、values-fr 和 values-en 的 strings.xml 应该更改所需引用的字符串,但是我单击单选按钮它不会更改引用仍然用英语,不要改成法语或西班牙语
I am developing a comic app.I have three radio buttons which are english,french and spanish respectively. And when the user clicks on any one of them the strings.xml from values-es,values-fr and values-en should change the string of the required references but and i click on the radio buttons it does not change the references are still in engish and do not change to french or spanish
推荐答案
像这样根据 Locale 改变你的语言:
Change your language according to Locale like this :
public class AndroidLocalize extends Activity {
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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 Es", Toast.LENGTH_SHORT)
.show();
setLocale("ta");
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"You have selected Fr", Toast.LENGTH_SHORT)
.show();
setLocale("hi");
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"You have selected En", Toast.LENGTH_SHORT)
.show();
setLocale("en");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
}
}
之后创建您更改的值文件夹.在您的情况下,来自 values-es、values-fr 和 values-en 的 strings.xml.
After that create values folder which you change. In your case strings.xml from values-es,values-fr and values-en.
这篇关于如何从内部更改android应用程序的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从内部更改android应用程序的语言?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
