安卓应用内切换语言

 原文地址:

http://snowpard-android.blogspot.com/2013/03/programmatically-change-language-in.html


不过

 Locale.setDefault(myLocale);
 这句可以去掉。

而且用

 android.content.res.Configuration config = getResources().getConfiguration();

就可以了,不需要新建对象。

更改应用语言后,通常需要刷新当前页面,即执行 

Intent refresh = new Intent(this, this.getClass()); 
    startActivity(refresh);
    finish();
 

但这样会引起页面跳动,所以在原文中用的是setTexts(),全部重新赋值一遍的方法。

如果需要更改语言后,按返回键在之前的页面也同样用新的语言设置,需要重写activity的onResume方法。



续:

测试时发现当APP运行时,去改变的系统的语言设置,后导致APP重新加载资源,locale就又成了系统的locale。

解决步骤:

1.在配置文件 AndroidManifest 的Application节点下中加入  android:configChanges="locale|layoutDirection", 这样当系统/activity的配置改变时,不会重新生成activity,而是去调用 onConfigurationChanged 方法。

当去改系统的配置时,会调用 Application的onConfigurationChanged()方法,参照http://blog.csdn.net/tangcheng_ok/article/details/7483993,你应该就知道怎么做了。

当是activity的配置改变时,会调用activity的onConfigurationChanged(),重载一下这个方法就好了。

上面提到的 android:configChanges的属性(locale,layoutDirection"等)如何跟各项配置映射到,我还没去了解过。测试结果是语言改变时,的确只是调用了onConfigurationChanged()方法,但地理位置等配置改变时,却不调用onConfigurationChanged()方法,也不重新生成activity.就测试而言, 这个方案目前是work fine...



不能翻墙的同学凑合看一下我复制的原文吧:

Programmatically change language in Android-application

Sometimes it is necessary in the application provide an opportunity the user to change the language of the application, regardless of the locale device. In this article I will show you how to do it.
Preparing resources
Create the string resources for four languages: English, Russian, French and German.
Source code values/strings.xml (English, Default)
< resources >
     < string  name = "app_name" > ChangeLocaleExample </ string >     
     < string  name = "hello_world" > Hello World </ string >
     < string  name = "btn_en" > English </ string >
     < string  name = "btn_ru" > Russian </ string >
     < string  name = "btn_de" > German </ string >
     < string  name = "btn_fr" > French </ string >
</ resources >

Source code values-ru/strings.xml (Russian)
< resources >
     < string  name = "hello_world" > Привет Мир </ string >
     < string  name = "btn_en" > Английский </ string >
     < string  name = "btn_ru" > Русский </ string >
     < string  name = "btn_de" > Немецкий </ string >
     < string  name = "btn_fr" > Французский </ string >
</ resources >

Source code values-fr/strings.xml (French)
< resources >    
     < string  name = "hello_world" > Bonjour monde </ string >
     < string  name = "btn_en" > Anglais </ string >
     < string  name = "btn_ru" > Russie </ string >
     < string  name = "btn_de" > Allemand </ string >
     < string  name = "btn_fr" > Français </ string >
</ resources >

Source code values-de/strings.xml (German)
< resources >    
     < string  name = "hello_world" > Hallo Welt </ string >
     < string  name = "btn_en" > Englisch </ string >
     < string  name = "btn_ru" > Russisch </ string >
     < string  name = "btn_de" > Deutsch </ string >
     < string  name = "btn_fr" > Französisch </ string >
</ resources >

Layout application will consist of four buttons that change the language of the application and the text box to display the greeting line:
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:orientation = "vertical"  >

     < TextView
         android:id = "@+id/txt_hello"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:layout_weight = "1"
         android:gravity = "center"
         android:text = "@string/hello_world"  />

     < LinearLayout
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:layout_weight = "1"
         android:orientation = "horizontal"
         android:gravity = "center" >

         < Button
             android:id = "@+id/btn_en"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_weight = "1"
             android:text = "@string/btn_en />
       
         < Button
             android:id = "@+id/btn_ru"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_weight = "1"
             android:text = "@string/btn_ru"  />
       
         < Button
             android:id = "@+id/btn_de"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_weight = "1"
             android:text = "@string/btn_de"  />
       
         < Button
             android:id = "@+id/btn_fr"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_weight = "1"
             android:text = "@string/btn_fr"  />
     </ LinearLayout >

</ LinearLayout >

Developing an application
Add attributes to our Activity.
  • UI elements:
private  TextView  txt_hello ;
private  Button  btn_en btn_ru btn_fr btn_de ;
  • Current local application:
private  Locale  myLocale ;
Develop necessary methods to change the language in the application.
  • Changing the language in the application:

public  void  changeLang(String lang)
{
     if  (lang.equalsIgnoreCase( "" ))
      return ;
     myLocale =  new  Locale(lang);
     saveLocale(lang);
     Locale.setDefault(myLocale);
     android.content.res.Configuration config =  new android.content.res.Configuration();
     config.locale = myLocale;
     getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
     updateTexts();
}

  • Save the current locale:

public  void  saveLocale(String lang)
{
     String langPref =  "Language" ;
     SharedPreferences prefs = getSharedPreferences( "CommonPrefs" , Activity.MODE_PRIVATE);
     SharedPreferences.Editor editor = prefs.edit();
     editor.putString(langPref, lang);
     editor.commit();
}

  • Loading a saved locale:

public  void  loadLocale()
{
     String langPref =  "Language" ;
     SharedPreferences prefs = getSharedPreferences( "CommonPrefs" , Activity.MODE_PRIVATE);
     String language = prefs.getString(langPref,  "" );
     changeLang(language);
}

  • Updating the UI elements of the current screen (you need to update only the screen in which a change of locale): 

private  void  updateTexts()
{
  txt_hello.setText(R.string.hello_world);
  btn_en.setText(R.string.btn_en);
  btn_ru.setText(R.string.btn_ru);
  btn_fr.setText(R.string.btn_fr);
  btn_de.setText(R.string.btn_de);
}

Add events to the buttons. For this implement OnClickListener interface for our Activity (implements OnClickListener). And implement the method onClick().

public  void  onClick(View v) {
   String lang =  "en" ;
   switch  (v.getId()) {
   case  R.id.btn_en:
    lang =  "en" ;
    break ;
   case  R.id.btn_ru:
    lang =  "ru" ;
    break ;
   case  R.id.btn_de:
    lang =  "de" ;
    break ;
   case  R.id.btn_fr:
    lang =  "fr" ;
    break ;  
   default :
    break ;
   }
   changeLang(lang);
  }

Finally, consistent with the work with the change of language in the method onCreate () of our Activity:

protected  void  onCreate(Bundle savedInstanceState) {
   super .onCreate(savedInstanceState);
   setContentView(R.layout.main);
   
   this .txt_hello = (TextView)findViewById(R.id.txt_hello);
   this .btn_en = (Button)findViewById(R.id.btn_en);
   this .btn_ru = (Button)findViewById(R.id.btn_ru);
   this .btn_fr = (Button)findViewById(R.id.btn_fr);
   this .btn_de = (Button)findViewById(R.id.btn_de);
   
   this .btn_en.setOnClickListener( this );
   this .btn_ru.setOnClickListener( this );
   this .btn_fr.setOnClickListener( this );
   this .btn_de.setOnClickListener( this );
   
   loadLocale();
  }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值