在Android手机的系统设置,语言与输入法中能够修改系统的语言,其实也可以在软件内实现本软件内语言的改变,比如大家熟悉的软件微信,在设置,通用,多语言,中进行相关的设置即可,下面贴出关键部分代码,以备用。
先看下效果图
代码段如下:
public void go(View v) { // 供选择改变的语言类别 String[] laStrs = new String[] { getResources().getString(R.string.string_lan_china), getResources().getString(R.string.string_lan_english), getResources().getString(R.string.string_lan_japanese) }; // 弹出的单选对话框 new AlertDialog.Builder(MainActivity.this) .setSingleChoiceItems(laStrs, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); if (which == 0) { Toast.makeText(MainActivity.this, MainActivity.this.getResources().getString(R.string.string_lan_china), Toast.LENGTH_SHORT).show(); Locale.setDefault(Locale.CHINESE); Configuration config = MainActivity.this.getResources().getConfiguration(); config.locale = Locale.CHINESE; MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics()); MainActivity.this.setContentView(R.layout.activity_main); // 刷新当前布局 } else if (which == 1) { Toast.makeText(MainActivity.this, MainActivity.this.getResources().getString(R.string.string_lan_english), Toast.LENGTH_SHORT).show(); Locale.setDefault(Locale.ENGLISH); Configuration config = MainActivity.this.getResources().getConfiguration(); config.locale = Locale.ENGLISH; MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics()); MainActivity.this.setContentView(R.layout.activity_main); } else if (which == 2) { Toast.makeText(MainActivity.this, MainActivity.this.getResources().getString(R.string.string_lan_japanese), Toast.LENGTH_SHORT).show(); Locale.setDefault(Locale.JAPAN); Configuration config = MainActivity.this.getResources().getConfiguration(); config.locale = Locale.JAPAN; MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics()); } } }).create().show(); }
在项目res文件夹下新建文件values-en文件夹,string.xml内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">In18</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="change_l">change language</string> <string name="go">go</string> </resources>
values文件夹下string.xml内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">语言国际化</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="change_l">改变语言</string> <string name="go">跳转</string> <string name="string_lan_china">中文</string> <string name="string_lan_english">英文</string> <string name="string_lan_japanese">日语</string> </resources>
希望对大家有用!