android APP国际化一键切换实现
Posted loaderman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android APP国际化一键切换实现相关的知识,希望对你有一定的参考价值。
首先看目录:
上代码:
package com.loaderman.language; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.Locale; public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getDefault().register(this); changeAppLanguage(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(String str) { switch (str) { case "EVENT_REFRESH_LANGUAGE": changeAppLanguage(); recreate();//刷新界面 break; } } public void changeAppLanguage() { String sta = Store.getLanguageLocal(this); if(sta != null && !"".equals(sta)){ // 本地语言设置 Locale myLocale = new Locale(sta); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
package com.loaderman.language; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; public class Store { public static void setLanguageLocal(Context context, String language){ SharedPreferences preferences; SharedPreferences.Editor editor; preferences = PreferenceManager.getDefaultSharedPreferences(context); editor = preferences.edit(); editor.putString("language", language); editor.commit(); } public static String getLanguageLocal(Context context){ SharedPreferences preferences; preferences = PreferenceManager.getDefaultSharedPreferences(context); String language = preferences.getString("language", ""); return language; } }
import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("第一个Activity"); findViewById(R.id.btn_setting).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SettingActivity.class)); } }); } }
package com.loaderman.language; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; public class SettingActivity extends BaseActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("设置Activity"); final String[] cities = {getString(R.string.lan_chinese), getString(R.string.lan_en), getString(R.string.lan_ja), getString(R.string.lan_de)}; final String[] locals = {"zh_CN", "en", "ja", "de"}; Button button = (Button)findViewById(R.id.btn_setting); button.setText("Language"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.select_language); builder.setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Store.setLanguageLocal(SettingActivity.this, locals[which]); EventBus.getDefault().post("EVENT_REFRESH_LANGUAGE"); } }); builder.show(); } }); } }
效果图:
以上是关于android APP国际化一键切换实现的主要内容,如果未能解决你的问题,请参考以下文章