场景: 在设置中切换中英文,然后跳转MainActivity.... 步骤: 1.在MyApplication 中初始化语言 String savedLanguage = LanguageUtil.getSavedLanguage(); if(TextUtils.isEmpty(savedLanguage)){ LanguageUtil.setLanguage("cn"); // 设置默认语言为 "cn" } 2.在 MainActiity 中 @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); } } 在 MainActiity 中 @Override onNewIntent()这个方法 , 如果不重写,当launch mode 为single task 时不会刷新 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_second); 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(); } } }); } } 设置界面的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", null); } }
时间: 2024-10-02 09:57:58