android开发中英文切换
2016-02-14 15:51
411 查看
今天介绍下安卓开发下面,如何实现中英文相互切换,
一,需要在res下新建文件夹,大陆中文为values-zh-rCN,英文values-en-rUS,更多可以参考http://my.oschina.net/quttap/blog/204499,直接用values-zh,values-en也可以,可以自己查询,然后其下新建strings.xml,(注:android默认访问values下strings.xml,当设置其它语言找不到对应文件夹时,将转而访问values下的strings.xml)
二,通过getResources().getConfiguration().locale.getLanguage()来获取当前应用语言,中文为zh,英文为en,更多可以参考http://blog.sina.com.cn/s/blog_7981f91f01012wm7.html
三,在androidmianfest.xml中配置下属性android:configChanges="locale",就是利用系统类设置语言,附主要代码,
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String sta=getResources().getConfiguration().locale.getLanguage();
shiftLanguage(sta);
}
//shift language
public void shiftLanguage(String sta){
if(sta.equals("zh")){
Locale.setDefault(Locale.ENGLISH);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = Locale.ENGLISH;
getBaseContext().getResources().updateConfiguration(config
, getBaseContext().getResources().getDisplayMetrics());
refreshSelf();
}else{
Locale.setDefault(Locale.CHINESE);
Configuration config = getBaseContext().get
4000
Resources().getConfiguration();
config.locale = Locale.CHINESE;
getBaseContext().getResources().updateConfiguration(config
, getBaseContext().getResources().getDisplayMetrics());
refreshSelf();
}
}
//refresh self
public void refreshSelf(){
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
效果图:
更多语言切换大家可举一返三
一,需要在res下新建文件夹,大陆中文为values-zh-rCN,英文values-en-rUS,更多可以参考http://my.oschina.net/quttap/blog/204499,直接用values-zh,values-en也可以,可以自己查询,然后其下新建strings.xml,(注:android默认访问values下strings.xml,当设置其它语言找不到对应文件夹时,将转而访问values下的strings.xml)
二,通过getResources().getConfiguration().locale.getLanguage()来获取当前应用语言,中文为zh,英文为en,更多可以参考http://blog.sina.com.cn/s/blog_7981f91f01012wm7.html
三,在androidmianfest.xml中配置下属性android:configChanges="locale",就是利用系统类设置语言,附主要代码,
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String sta=getResources().getConfiguration().locale.getLanguage();
shiftLanguage(sta);
}
//shift language
public void shiftLanguage(String sta){
if(sta.equals("zh")){
Locale.setDefault(Locale.ENGLISH);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = Locale.ENGLISH;
getBaseContext().getResources().updateConfiguration(config
, getBaseContext().getResources().getDisplayMetrics());
refreshSelf();
}else{
Locale.setDefault(Locale.CHINESE);
Configuration config = getBaseContext().get
4000
Resources().getConfiguration();
config.locale = Locale.CHINESE;
getBaseContext().getResources().updateConfiguration(config
, getBaseContext().getResources().getDisplayMetrics());
refreshSelf();
}
}
//refresh self
public void refreshSelf(){
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
效果图:
更多语言切换大家可举一返三
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories