android 在应用中切换语言
场景: 在设置中切换中英文,然后跳转MainActivity....
步骤:
1.在MyApplication 中初始化语言
public class MyApplication extends Application { private static MyApplication instance; public static MyApplication getInstance() { return instance; } public static MyApplication getContext() { return instance; } @Override public void onCreate() { super.onCreate(); instance = this; String savedLanguage = LanguageUtil.getSavedLanguage(); if (TextUtils.isEmpty(savedLanguage)) { LanguageUtil.setLanguage("cn"); // 设置默认语言为 "cn" } else { LanguageUtil.setLanguage(savedLanguage); } } }
2.在 MainActivity 中 @Override onNewIntent()这个方法 , 如果不重写,当launch mode 为single task 时不会刷新
@Override protected void onNewIntent(Intent intent) { if (intent.getAction() == "language changed") { finish(); startActivity(new Intent(this, MainActivity.class)); } else { super.onNewIntent(intent); } }
3.设置界面代码和布局文件
public class SettingActivity extends BaseActivity { RadioGroup rgLanguage; RadioButton rbCn; RadioButton rbEn; private int checkedRadioButtonId; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting_language); rgLanguage = super.$(this, R.id.rgLanguage); rbCn = super.$(this, R.id.rbCn); rbEn = super.$(this, R.id.rbEn); if ("en".equals(LanguageUtil.getSavedLanguage())) { rbEn.setChecked(true); } else { rbCn.setChecked(true); } checkedRadioButtonId = rgLanguage.getCheckedRadioButtonId(); super.$(this, R.id.btnConfirm).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkedRadioButtonId != rgLanguage.getCheckedRadioButtonId()) { if (rbCn.isChecked()) { LanguageUtil.setLanguage("cn"); } else if (rbEn.isChecked()) { LanguageUtil.setLanguage("en"); } finish(); Intent intent = new Intent(MyApplication.getContext(), FirstActivity.class); intent.setAction("language changed"); startActivity(intent); } else { finish(); } } }); } }
////////////////////////////////////////////////////////////////////
public class BaseActivity extends Activity {
public <T extends View> T $(Activity activity, int resId) {
return (T) activity.findViewById(resId);
}
}
设置界面 activity_setting_language.xml 布局
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/switcher"/> <RadioGroup android:id="@+id/rgLanguage" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/rbCn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="中文"/> <RadioButton android:id="@+id/rbEn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="English"/> </RadioGroup> <Button android:id="@+id/btnConfirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/confirm"/> </LinearLayout>
4.LanguageUtil 代码
public class LanguageUtil { /** * 设置语言 * * @param language "cn" = 中文, "en" = 英文 */ public static void setLanguage(String language) { Resources resources = MyApplication.getContext().getResources(); Configuration configuration = resources.getConfiguration(); configuration.locale = new Locale(language); resources.updateConfiguration(configuration, null); saveLanguage(language); } public static void saveLanguage(String language) { MyApplication.getContext().getSharedPreferences("settings", Context.MODE_PRIVATE) .edit().putString("language", language).commit(); } public static String getSavedLanguage() { return MyApplication.getContext().getSharedPreferences("settings", Context.MODE_PRIVATE) .getString("language", ""); } }
5.AndroidManifest.xml 中添加权限,以及MyApplication的注册
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>