首先看一下效果图:
项目结构图:
Android 3.0以下版本的做法:
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class HelloMyL10NActivity extends Activity {
private SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if(savedInstanceState==null){
// savedInstanceState = getIntent().getBundleExtra("Instance");
// if(savedInstanceState!=null){
// onRestoreInstanceState(savedInstanceState);
// }
// }
preferences = getSharedPreferences("lang",Context.MODE_PRIVATE);
String langString = preferences.getString("lang", "en");
setLanguage(langString);
setContentView(R.layout.main);
Button b;
(b = (Button)findViewById(R.id.flag_button)).setBackgroundDrawable( this.getResources().getDrawable(R.drawable.flag));
// 语言选择
final String lang[] = new String[]{"en","fr","de","ja"};
String items[] = new String[]{"英文","法文", "德文", "日文"};
Spinner spinner = (Spinner)findViewById(R.id.chooseLang);
spinner.setAdapter( new ArrayAdapter<String>( this,android.R.layout.simple_spinner_item,items));
if(preferences.getInt("lang",0) != 0){
spinner.setSelection(savedInstanceState.getInt("position"), true);
}
spinner.setOnItemSelectedListener( new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(!(preferences.getString("lang", "en").equals(lang[position]))){
setLanguage(lang[position]);
Editor editor = preferences.edit();
editor.putString("lang", lang[position]).commit();
Intent intent = new Intent(HelloMyL10NActivity. this,HelloMyL10NActivity. this.getClass());
startActivity(intent);
finish();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
private void setLanguage(String langString){
Resources resources = getResources();
Configuration config = resources.getConfiguration();
Locale locale = new Locale(langString);
Locale.setDefault(locale);
config.locale = locale; // 简体中文
resources.updateConfiguration(config, null);
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class HelloMyL10NActivity extends Activity {
private SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if(savedInstanceState==null){
// savedInstanceState = getIntent().getBundleExtra("Instance");
// if(savedInstanceState!=null){
// onRestoreInstanceState(savedInstanceState);
// }
// }
preferences = getSharedPreferences("lang",Context.MODE_PRIVATE);
String langString = preferences.getString("lang", "en");
setLanguage(langString);
setContentView(R.layout.main);
Button b;
(b = (Button)findViewById(R.id.flag_button)).setBackgroundDrawable( this.getResources().getDrawable(R.drawable.flag));
// 语言选择
final String lang[] = new String[]{"en","fr","de","ja"};
String items[] = new String[]{"英文","法文", "德文", "日文"};
Spinner spinner = (Spinner)findViewById(R.id.chooseLang);
spinner.setAdapter( new ArrayAdapter<String>( this,android.R.layout.simple_spinner_item,items));
if(preferences.getInt("lang",0) != 0){
spinner.setSelection(savedInstanceState.getInt("position"), true);
}
spinner.setOnItemSelectedListener( new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(!(preferences.getString("lang", "en").equals(lang[position]))){
setLanguage(lang[position]);
Editor editor = preferences.edit();
editor.putString("lang", lang[position]).commit();
Intent intent = new Intent(HelloMyL10NActivity. this,HelloMyL10NActivity. this.getClass());
startActivity(intent);
finish();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
private void setLanguage(String langString){
Resources resources = getResources();
Configuration config = resources.getConfiguration();
Locale locale = new Locale(langString);
Locale.setDefault(locale);
config.locale = locale; // 简体中文
resources.updateConfiguration(config, null);
}
}
这种方法来到Android 4.0就行不通,文字的效果还是有国际化但是图片就没有了国际化的效果。
推荐一个博客http://blog.csdn.net/a_colt/article/details/6193857 ,看一下它里面说的这个问题你有没有存在
“但同时发现了个问题
如果是用 button 的事件来触发
对于同一activity 中已生成的 目录按钮 不起作用,但对于触发前还没生成的对话框却起作用,是不是在触发后需要刷新下以生成的目录菜单呢? ”
最后上传一个国际化转化的工具类,用的时候需要以下配置: /Files/lee0oo0/LanguageSettingUtil.rar
LanguageSettingUtil.init(getActivity());
languageSetting = LanguageSettingUtil.get();
//拿到当前系统语言设置
final String curSysLanguage = Locale.getDefault().getLanguage();
languageSetting.checkSysChanged(curSysLanguage);
// 设置更换的语言
languageSetting.saveLanguage("zh");
// 更新
languageSetting.refreshLanguage();